Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda