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

187
Views
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 answers
Answer question

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 Report

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 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!