Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda