这是我的模型。在这里,我正在从服务器接收一个名为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();
  }

最佳答案

如果需要在对象键名中使用dot(.)字符,则需要像对字符串一样用double("")single('')引号将其括起来。

所以像这样写你的代码-

"Father.Firstname" : response.fatherfname,


代替

Father.Firstname : response.fatherfname,

10-04 22:29
查看更多