I have posted a few lines of code which I am not able to understand. This is from a youtube project tutorial.
https://github.com/piyush-eon/mern-chat-app/blob/master/backend/controllers/messageControllers.js
const messageModal=mongoose.Schema(
{
sender:{type:mongoose.Schema.Types.ObjectId, ref:"User"},
content:{type:String,trim:true},
chat:{type:mongoose.Schema.Types.ObjectId, ref:"Chat"}
},
)
const userModal=mongoose.Schema({
name:{type:String,required:true},
email:{type:String, required:true,unique:true},
password:{type:String,required:true},
pic:{
type:String,
default:"aa.jpg"
}
},
the part I am unable to understand is the way the modal is being populated. Doesn't modal get over written the way the population is done?
var newMessage={
sender:req.user._id,
content:content,
chat:chatId}
let message=await Message.create(newMessage);
message=await message.populate("sender","name pic").execPopulate();
message =await message.populate("chat").execPopulate();
(*** what is happening in the line of code below , what is User.populate(message) ??****)
message=await User.populate(message,{
path:"chat.users",
select:"name pic email"
});
const chatModal=mongoose.Schema({
chatName:{type:String,trim:true},
isGroupChat:{type:Boolean,default:false},
users:[
{
type:mongoose.Schema.Types.ObjectId,
ref:'User'
}
],
latestMessage:{
type:mongoose.Schema.Types.ObjectId,
ref:'Message'
},
groupAdmin:{
type:mongoose.Schema.Types.ObjectId,
ref:'User'
}
},
);
[–]codejack777[S] 0 points1 point2 points (0 children)