这是我的模型。我在服务器上收到一个名为response的对象。现在我需要使用此响应对象更新数据库。 但问题是我只能更新变量而不能像父亲那样更新对象。 父亲是一个对象现在我需要更新父亲的名字。但如果我使用Father.Firstname说出意外的令牌,它会给我错误。 请帮我解决这个问题。
var User = mongoose.model('User', userSchema);
function createStudent(response) {
console.log(response);
var list = new User({
Firstname : response.Fname,
Age : response.age,
Lastname : response.Lname,
Father.Firstname : response.fatherfname,
Father.Lastname : response.fatherlname,
Father.Occupation : response.occupation,
Father.PlaceOfWork : response.placeofwork,
Father.OfficialAddress : response.officaladd,
Father.EmailId : response.emailid,
Father.PhoneNo : response.phoneno,
Father.MobileNo : response.mobileno,
});
list.save();
}
答案 0 :(得分:1)
如果您需要在对象键名称中使用dot(.)
字符,则需要使用double("")
或single('')
引号将其括起来,就像使用字符串一样。
所以写这样的代码 -
"Father.Firstname" : response.fatherfname,
而不是
Father.Firstname : response.fatherfname,