【发布时间】:2023-02-26 21:14:00
【问题描述】:
当我尝试调用下面的端点时,我从邮递员那里得到了下面的错误。
{
"success": false,
"error": "Unexpected token / in JSON at position 7"
}
- 在
addFollowing函数中你可以看到我已经尝试记录userId和followingId但我没有从我的终端收到任何日志 - 接下来我尝试使用这个包
npm i json-sanitizer来清理req.body,但都无济于事
const addFollowing = async (req, res, next) => {
const userId = req.body.userId;
const followId = req.body.followId;
console.log(userId, followId);
try {
// Update the user's following list
const updatedUser = await User.findByIdAndUpdate(
userId,
{ $addToSet: { following: followId } },
{ new: true }
).select("username email following followers");
if (!updatedUser) {
return next(new ErrorResponse(404, "User not found"));
}
// Update the follower's followers list
const updatedFollower = await User.findByIdAndUpdate(
followId,
{ $addToSet: { followers: userId } },
{ new: true }
).select("username email following followers");
if (!updatedFollower) {
// Roll back the previous update to the user's following list
await User.findByIdAndUpdate(userId, { $pull: { following: followId } });
return next(new ErrorResponse(404, "User not found"));
}
// Return the updated user and follower objects
res.json({
user: updatedUser,
follower: updatedFollower,
});
} catch (err) {
next(err);
}
};
我第一次在编写后确实调用了这个端点,它是成功的,但随后的尝试导致了上述错误,我一直在努力解决这个问题。 Assuch 非常感谢您的帮助。谢谢
这也是我的路线
router.post("/add-following",protect, addFollowing);
【问题讨论】:
-
所以这意味着数据没有到达您的服务器,对吗?你试过使用postman raw json格式发送http post方法吗
-
@NaveenRavi 在为此奋斗了几个小时之后,我决定使用 chrome 扩展程序 thunderClient 而不是邮递员......并且它有效。仍在尝试弄清楚我的邮递员到底出了什么问题
标签: javascript node.js mongodb express mern