英文:
how do I fix 'cannot post error' in nodejs?
问题
Method: post
Url: localhost:8080/api/v1/users/:userid/subscribe/:businessid
AC: 当前用户的关注数组中添加商家id。
将当前用户id添加到商家的关注者数组中。
最终返回状态码为201,{user}
但每次我在Postman中尝试测试它时,都会收到以下错误:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A</pre>
</body>
</html>
我不确定哪里出错了。
我的用户模型:
import { Request, Response } from 'express';
import UserModel from '../models/User';
import mongoose from 'mongoose';
export const subscribe = async (req: Request, res: Response) => {
const { userid, businessid } = req.params;
// 搜索用户id
const user = await UserModel.findById(userid).exec();
// 搜索商家id
const business = await UserModel.findById(businessid).exec();
if(!user || !business){
res.status(404).json({ error: '找不到用户或商家' });
return;
}
if (!user.is_business) {
return res.status(403).json({ message: '用户不是商家' });
}
business.follower.addToSet(userid);
await business.save();
user.following.addToSet(businessid);
await user.save();
return res.status(201).json({ user });
};
用户模型:
import { Schema, model } from 'mongoose';
const schema: Schema = new Schema({
is_business: {
type: Boolean,
required: true,
default: false,
},
following: [{
type: Schema.Types.ObjectId,
ref: "User",
}],
follower: [{
type: Schema.Types.ObjectId,
ref: "User",
}],
},
{
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at',
}
});
export default model('User', schema);
我的用户路由:
import { Router } from 'express';
import { subscribe } from '../../controllers/users';
const userRouter = Router();
userRouter.post('/users/subscribe/:userid/:businessid', subscribe);
export default userRouter;
有谁知道如何修复它?感谢任何帮助!
英文:
new to node.js
I'm working on a nodejs-experss-mongodb project and I'm currently implementing a subscription function with the following specific requirements:
Method: post
Url:localhost:8080/api/v1/users/:userid/subscribe/:businessid
AC:
Current user's following array, add business id.
Add the current user id to the merchant's follower array
The final return is 201, {user}
But every time I try to test it in postman, I get this error:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A</pre>
</body>
</html>
I'm not sure where it went wrong.
My usermodels:
import { Request, Response } from 'express';
import UserModel from '../models/User';
import mongoose from 'mongoose';
export const subscribe = async (req: Request, res: Response) => {
const { userid, businessid } = req.params;
// search for student id
const user = await UserModel.findById(userid).exec();
// search for business id
const business = await UserModel.findById(businessid).exec();
if(!user || !business){
res.status(404).json({ error: 'user or business not found' });
return;
}
if (!user.is_business) {
return res.status(403).json({ message: 'User is not a business' });
}
business.follower.addToSet(userid);
await business.save();
user.following.addToSet(businessid);
await user.save();
return res.status(201).json({ user });
};
usermodels:
import { Schema, model } from 'mongoose';
const schema: Schema = new Schema({
is_business: {
type: Boolean,
required: true,
default: false,
},
following: [{
type: Schema.Types.ObjectId,
ref: "User",
},],
follower: [{
type: Schema.Types.ObjectId,
ref: "User",
}],
},
{
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at',
}
});
export default model('User', schema);
my user routes:
import { Router } from 'express';
import { index, deactivate, activate, subscribe } from '../../controllers/users';
const userRouter = Router();
userRouter.post('/users/subscribe/:userid/:businessid', subscribe);
export default userRouter;
Anyone knows how to fix it? Any helps are appreciated!
答案1
得分: 1
你的路由定义如下:
'/users/subscribe/:userid/:businessid'
但是,你的URL被发送为:
'/users/:userid/subscribe/:businessid'
这两者不匹配。请更改其中一个以使其与另一个匹配。
英文:
Your route is defined as:
'/users/subscribe/:userid/:businessid'
But, your URL is sent as:
'/users/:userid/subscribe/:businessid'
These do not match. Change one of them to match the other.
答案2
得分: 0
请检查您正在重定向到的URL,我认为您已经编写了以下内容:
- /api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A
请重定向到以下内容:
/api/v1/users/(subscribe_id)/647965651bbc93663ec695d5/:businessid
英文:
Please check the URL which are you redirecting to I think
you have written these
-
/api/v1/users/647965651bbc93663ec695d5/subscribe/647965651bbc93663ec695d6%0A
-
please redirect into these
/api/v1/users/(subscribe_id)/647965651bbc93663ec695d5/:businessid
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论