M
M
Mister_krid2021-06-23 11:21:52
MongoDB
Mister_krid, 2021-06-23 11:21:52

How to send user id?

I set up a chat for the site, created a model for mongo and a function for sending data, but for some reason the user id does not come, although a chat room with its id is created.
Model

const mongoose = require("mongoose");

const ConversationSchema = new mongoose.Schema(
  {
    members: {
      type: Array,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("Conversation", ConversationSchema);

create function
router.post("/", async (req, res) => {
  const newConversation = new Conversation({ /* использует модель  Conversation*/
    members: [req.body.senderId, req.body.receiverId], /*уч. беседы:тело запроса[id.sende,id.rece]  */
  });

  try {
    const savedConversation = await newConversation.save();
    res.status(200).json(savedConversation);
  } catch (err) {
    res.status(500).json(err);
  }
});

there are users with the specified id in the database
60d2ef1b58cfd967303965.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bondaf, 2021-06-24
@Mister_krid

Judging by the fact that in the response there are two values ​​in the members field (both null), I can assume that something is still wrong with getting values ​​from body.
Output req.body.senderId, req.body.receiverId and req.body itself to the console
On the screen, it seems that something is wrong with quotes...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question