I am trying to relationships todo and user, my model is something like this
user = new Schema({
email: {
type: String,
unique: true,
required: [true, "must has email"]
},
password: {
type: String,
required: [true, "fill your password"]
},
name: {
type: String,
required: [true, "must fill your name"]
},
task: [{ type: Schema.Types.ObjectId, ref: "Todo" }]
todo = owned_id: { type: Schema.Types.ObjectId, ref: "User" },
task: {
type: String,
required: [true, "please fill your taks"]
},
done: { type: Boolean, default: false }
I create the task and get the owned_id by req.session.user which I save on session when the user login, it something like this, user add task then I fill the owned_id: req.session.user ( because I save their Object_id on session) then I tried to get the populate of the Todo user login, something like this
User.find({_id: req.session.user}).populate('task')
it doesn't't show anything,
and I tried on todo collection again, something like this,
Todo.find({owned_id: req.session.id}).populate('task')
it only show the user id , and the task is empty array
I really want to get the result of user login tasks, am I wrong to creat collections ?? or wrong on to use populate?
when I create task, I just put the owned_id that I got from req.session.user, the task on user, still empty, should I update the task it by the newTask_id when user create todo task ?
here is my data for todo collection
_id:5ca9ae3cd4198bdb3d68e93e
done:false
owned_id:5ca9a000932528c98fcc89cb
task:"doing something"
__v:0
and here is the user which create the task
_id:5ca9a000932528c98fcc89cb
task:Array
email:"dave@email.com"
password:"12345678"
name:"David Lee"
__v:0