javascript - Update a document with mongoose (update property of document property) - Stack Overflow

I would like to update the document of a user with form inputs using mongoose. Inside the user document

I would like to update the document of a user with form inputs using mongoose. Inside the user document I would like to access the personal info section (and in this case its fullName property) to update it with the form's data. I tried with personalInfo.fullName in the mongoose update function, but this doesn't seem to work. Can anyone fix this?

router.post('/personalInfo', function (req, res, next) {


    User.update({username: req.user.username}, {$set: { personalInfo.fullName: req.body.fullName}}, function (err, user) {
        if (err) throw error
        console.log(user);
        console.log("update user plete")
    })
});

I would like to update the document of a user with form inputs using mongoose. Inside the user document I would like to access the personal info section (and in this case its fullName property) to update it with the form's data. I tried with personalInfo.fullName in the mongoose update function, but this doesn't seem to work. Can anyone fix this?

router.post('/personalInfo', function (req, res, next) {


    User.update({username: req.user.username}, {$set: { personalInfo.fullName: req.body.fullName}}, function (err, user) {
        if (err) throw error
        console.log(user);
        console.log("update user plete")
    })
});
Share Improve this question asked Oct 17, 2017 at 16:15 Tijl DeclerckTijl Declerck 1052 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Try adding quotes around "personalInfo.fullName":

User.update({
  username: req.user.username
}, {
  $set: { 
    "personalInfo.fullName": req.body.fullName
  }
}, function (err, user) {
    if (err) throw err
    console.log(user)
    console.log("update user plete")
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745364074a4624494.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信