I am a beginner and i want to create follow/unfollow functionality in my app.
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);
})})
when running the code it recieves the message "following user" but dosen't add to the database i.e. Mongodb (i'm using MERN stack)
It seems like this block of code is not executing.
req.userInfo.updateOne({$push: {followings: req.params.id}});
otherUser.updateOne({ $push: {followers: req.userInfo._id}});
what am i doing wrong here?