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

229
Views
¿Cómo empujar cada elemento dentro de una matriz de objetos en mangosta?

Estoy tratando de actualizar cada elemento dentro de una matriz con Mongoose. Lo que me gustaría tener es que cada usuario invitado a un evento tenga el evento creado dentro de una matriz. Pero recibo un error SyntaxError: Unexpected token '.' al intentar un bucle for. ¿Alguien tiene una solución?

Este es mi código:

 router.post("/new-event", (req, res, next) => { const { title, organiser, invitedPeople, } = req.body Event.create({ title, organiser, invitedPeople, }) .then(createdEvent => { User.findByIdAndUpdate( organiser, { $push: { organisedEvents: createdEvent } }, { new: true } ).then(updatedUser => { for (let i = 0; i < invitedPeople.length; i++) { User.findByIdAndUpdate( invitedPeople[i], { $push: { invitedEvents: createdEvent } }, { new: true } ) } .then(updatedUsers => { res.status(200).json({ user: updatedUser, createdEvent, updatedUsers, }) }) }) }) .catch(err => next(err)) })

El resto funciona bien, solo que este $push dentro del bucle for no funciona.

¡Gracias por tus respuestas!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede actualizarlos todos con una consulta. No necesitas un bucle for .

En lugar de hacer esto:

 for (let i = 0; i < invitedPeople.length; i++) { User.findByIdAndUpdate( invitedPeople[i], { $push: { invitedEvents: createdEvent } }, { new: true } ) }

Puedes hacerlo:

 User.update( { _id: { "$in": invitedPeople }}, { $push: { invitedEvents: createdEvent } }, { new: true } )
over 4 years ago · Santiago Trujillo 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!