Home > Software engineering >  Express doesn't get another user with .map()
Express doesn't get another user with .map()

Time:10-21

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related