'How can Add two different field which type is ObjectId in same object which refer same schema
const ChatSchema = new mongoose.Schema({
username: {
type: String,
},
mobile: {
type: Number,
},
friends: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "ChatSchema",
},
],
chating: [
{
senderId: {
type: mongoose.Schema.Types.ObjectId,
ref: "ChatSchema",
},
recieverId: {
type: mongoose.Schema.Types.ObjectId,
ref: "ChatSchema",
},
text: {
type: String,
},
},
],
});
module.exports = mongoose.model("ChatSchema", ChatSchema);
when i update my schema with necessary data it give **error : maximum stack size exceed. **
const data = await ChatSchema.findById(req.params.id);
req.userData.chating.push({
senderId: req.userData, //if i replaced req.userData with req.userData._id it works.
text: req.body.message,
recieverId: data, //if i replaced data with data._id it works.
});
await req.userData.save();
data.chating.push({
senderId: req.userData, //if i replaced req.userData with req.userData._id it works.
text: req.body.message,
recieverId: data, //if i replaced data with data._id it works.
});
await data.save();
res.status(200).json({
data: req.userData,
error: false,
});
Note: when i remove any on either recieverID or senderId from schema then it work fine with req.userId or data instead of req.userId._id or data._id respectively.
If any solution is possible then reply me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|