我正在写以下请求以取消关注用户。追随者作为数组添加到数据库中。
router.put("/:id/unfollow",async (req,res) => {
if(req.body.userId !== req.params.id){
try{
const user = await User.findById(req.params.id);
const currentUser = await User.findById(req.params.id);
if (user.followers.includes(req.body.userId)){
await user.updateOne({$pull:{followers:req.body.userId}});
await currentUser.updateOne({$pull:{followers:req.params.id}});
res.status(200).json("user has been unfollowed");
}else{
res.send(403).json("you already unfollowed this user ");
}
}catch (err){
res.status(500).json(err);
console.log("err");
}
} else{
res.status(403).json("You cant unfollow yourself")
}
})
当通过 postamn 使用正确的 userId 发送请求时,邮递员说“禁止”
并且终端显示以下错误。这里有什么问题?
express deprecated res.send(status): Use res.sendStatus(status) instead routes\user.js:98:21
(node:15336) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at ServerResponse.header (C:\Users\Banchi\Desktop\ReactTest\moodfix-api2\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\Users\Banchi\Desktop\ReactTest\moodfix-api2\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\Banchi\Desktop\ReactTest\moodfix-api2\node_modules\express\lib\response.js:267:15)
at C:\Users\Banchi\Desktop\ReactTest\moodfix-api2\routes\user.js:102:29
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:15336) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a prom
ise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#c
li_unhandled_rejections_mode). (rejection id: 1)
(node:15336) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-z
ero exit code.