Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Implement follow/unfollow in Node.js

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think that is because req.userInfo and otherUser is an immutable mongo object. try this instead (assuming "user" is your model object):

user.updateOne(
   { _id:  req.userInfo._id},
   {$push: {followings: req.params.id}
)

  user.updateOne(
       { _id:  otherUser._id},
       { $push: {followers: req.userInfo._id}}
    )

note: while comparing _id please check if you are storing as objectId or string.

for your reference: Comparing mongoose _id and strings

about 4 years ago · Juan Pablo Isaza Report

0

Well, this seems to work

router.put('/user/:id/follow', auth.verifyuser, (req, res)=>{

user.findById(req.params.id)
.then((otherUser)=>{
    if(!otherUser.followers.includes(req.userInfo._id)){
        Promise.all([
            user.updateOne({_id:req.userInfo._id}, {$push: {followings: req.params.id}}),
            user.updateOne({_id: otherUser._id},{$push: {followers: req.userInfo._id}})
        ]).then(()=>{
            res.json({message: "following user"});
        })
        .catch((e)=>{
            res.json(e);
        }); 
    }else{
        return res.json({message: "Already following user"});
    }
})
});

But it would be better if someone explained what went wrong previously, as I said i am beginner and would appreciate the feedback.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!