我正在尝试与待办事项和用户建立关系, 我的模特是这样的
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 }
我创建任务,并通过req.session.user获取owner_id,该用户在用户登录时保存在会话中, 像这样,用户添加任务,然后我填写了owned_id:req.session.user(因为我在会话中保存了他们的Object_id) 然后我尝试填充Todo用户登录名, 像这样
User.find({_id: req.session.user}).populate('task')
它什么也没显示,
然后我再次尝试了todo收集,类似这样,
Todo.find({owned_id: req.session.id}).populate('task')
仅显示用户ID,任务为空数组
我真的很想得到用户登录任务的结果, 我创建收藏集不对吗?还是使用填充错误?
当我创建任务时,我只是放入从req.session.user获得的owner_id, 用户的任务,仍然为空, 用户创建待办任务时,应该通过newTask_id更新任务吗?
这是我的待办事项收集数据
_id:5ca9ae3cd4198bdb3d68e93e
done:false
owned_id:5ca9a000932528c98fcc89cb
task:"doing something"
__v:0
这是创建任务的用户
_id:5ca9a000932528c98fcc89cb
task:Array
email:"dave@email.com"
password:"12345678"
name:"David Lee"
__v:0