【发布时间】:2022-02-11 16:47:35
【问题描述】:
我是初学者,我想在我的应用中创建关注/取消关注功能。
router.put('/user/:id/follow', auth.verifyuser, (req, res)=>{
user.findById(req.params.id)
.then((otherUser)=>{
if(otherUser.followers.includes(req.userInfo._id)){
return res.json({message: "Already following user"});
}
req.userInfo.updateOne({$push: {followings: req.params.id}});
otherUser.updateOne({ $push: {followers: req.userInfo._id}});
res.json({message: "following user"});
console.log(otherUser.followers);
})
.catch((e)=>{
res.json(e);
})})
运行代码时,它会收到“关注用户”的消息,但不会添加到数据库,即 Mongodb(我正在使用 MERN 堆栈)
这段代码似乎没有执行。
req.userInfo.updateOne({$push: {followings: req.params.id}});
otherUser.updateOne({ $push: {followers: req.userInfo._id}});
我在这里做错了什么?
【问题讨论】:
标签: javascript node.js express mongoose mern