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

195
Views
Implementar seguir/dejar de seguir en Node.js

Soy un principiante y quiero crear la funcionalidad de seguir/dejar de seguir en mi aplicación.

 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); })})

al ejecutar el código, recibe el mensaje "siguiendo al usuario" pero no se agrega a la base de datos, es decir, Mongodb (estoy usando la pila MERN)

Parece que este bloque de código no se está ejecutando.

 req.userInfo.updateOne({$push: {followings: req.params.id}}); otherUser.updateOne({ $push: {followers: req.userInfo._id}});

¿Qué estoy haciendo mal aquí?

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

0

Creo que eso se debe a que req.userInfo y otherUser es un objeto mongo inmutable. intente esto en su lugar (suponiendo que "usuario" sea su objeto modelo):

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

nota: al comparar _id, verifique si está almacenando como objectId o cadena.

para su referencia: Comparando mongoose _id y cadenas

about 4 years ago · Juan Pablo Isaza Report

0

Bueno, esto parece funcionar

 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"}); } }) });

Pero sería mejor si alguien explicara qué salió mal anteriormente, como dije, soy principiante y agradecería los comentarios.

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!