我有一个获取request.user属性并更新其值的函数,如下面的代码所示。问题在于更新后的文档不会保存,但是我可以使用更新后的版本将其记录到控制台中
async function checkPosts(){
if(req.user.get("posts")){
let posts = await req.user.get("posts");
posts.push({
"Type": "Idea",
"Field/s of idea": req.body["Field/s of idea"],
"Idea": req.body.idea,
"Date": new Date()
});
await req.user.set("posts", posts);
}else{
await req.user.set("posts", [{
"Type": "Idea",
"Field/s of idea": req.body["Field/s of idea"],
"Idea": req.body.idea,
"Date": new Date()
}]
);
};
User.findOne({email:req.user.email})
.then(user=>{
//also tried req.user.save();
user=req.user;
user.save();
})
.catch(err=>{
if(err) throw err
})
}
在猫鼬模式中未定义posts property
。
这是模式:
const UserSchema = new Schema({
_id: {type: ObjectId, auto: true},
username: {type: String, required: true, max:18},
email: {type: String, required: true},
password: {type: String, required: true, min:5},
date_of_registration: {type: Date, required: true}
}, { strict: false });