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

193
Vistas
Express doesn't get another user with .map()

I came to a problem, where I can create conversations with multiple people 2 and so on. However, I can't understand why it doesn't store data to seperate User models.

Here is a code that you only need to know:

router.post(
  "/",
  auth,
  [
    check("conversators", "There should be at least two conversators").isLength(
      { min: 2 }
    ),
  ],
  async (req, res) => {
    const { conversators } = req.body;
    const errors = validationResult(req);

    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }

    try {
      let conversation = new Conversation({
        user: req.user.id,
        conversators: conversators,
      });

      await conversators.map(async (conversator) => {
        let user = await User.findById(conversator);
        let newData = user;
        newData.conversations.push(conversation.id);

        console.log('Created data', newData);

        let newUser = await User.findOneAndUpdate(
          { user: conversator },
          {
            $set: {
              newData,
            },
          },
          { new: true }
        );

        await newUser.save();
        console.log(newUser);
      });

      await conversation.save();
      res.status(200).json(conversation);
    } catch (error) {
      console.error(error.message);
      res.status(500).send("Server error.");
    }
  }
);

module.exports = router;

What I can assure is that this line: console.log('Created data', newData); prints the desired data. However, the next console: console.log(newUser); prints the same User model as the previous one.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  name: {
    type: String,
    required: true,
  },
  surname: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
  },
  conversations: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "conversation",
    },
  ],
  date: {
    type: Date,
    default: Date.now,
  },
});

module.exports = User = mongoose.model("user", UserSchema);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The reason might be the difference in search methods used to get a record for newData and newUser. You have used User.findById for newData, which will obviously return different objects for different ids. But User.findOneAndUpdate uses filter criteria that may satisfy several results, but only first will be returned. So it boldly depends on what that user field is.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is the part that I changed and started to see the data on MongoDB:

await conversators.map(async (conversator) => {
  let user = await User.findById(conversator);
  let newData = user;
  newData.conversations.push(conversation.id);
  
  new Promise(async (resolve, reject) => {
    user = await User.findOneAndUpdate(
      { id: conversator },
      {
        $set: {
          newData,
        },
      },
      { new: true }
    );
    return resolve;
  })

  return await user.save();
});

Posted on behalf of the question asker

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