【问题标题】:Unexpected token node express issue意外的令牌节点表达问题
【发布时间】: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


【解决方案1】:

它在 thunderclient 上工作得很好,但在邮递员上却没有。仍然不明白为什么但试图。但如果将来有人遇到这样的问题,请尝试使用 vscode 扩展 thunderclient

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2015-12-16
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多