Abhilash Muttalli Abhilash Muttalli - 4 years ago 110
Node.js Question

need to update values inside an object using mongoose

this is my model.Here i am receiving an object from the server called response. Now i need to update the data base with this response object.
But the problem is i can only update the variables but not the objects like father.
Father is one object now i need to update father's firstname. But its giving me error if i use Father.Firstname saying unexpected token.
please help me out how to overcome this problem..

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();
}

Answer Source

If you need to use the dot(.) character in you object key names then you need to enclose it withing double("") or single('') quotes like you do with strings.

So write your code like this -

"Father.Firstname" : response.fatherfname,

instead of

Father.Firstname : response.fatherfname,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download