Wednesday, 10 May 2017

Update member in mongoose

I am using passport.js and I'd like to update the profile for the currently logged in Member and save it to the database. Right now I am using save but I am worried that this could lead to issues when someone else modifies this profile at the same time. Instead I would like to use MongoDB's upsert feature.

function updateProfile(req, res) {
    var user = req.user
    var form = req.body
    var profile = {
        name: form.profileRealName,
        bio: form.profileBio,
        url: form.profileUrl,
        location: form.profileLocation
    }

    user.profile = profile
    logger.info(user)

    user.save(function(err) {
        if(err)
            next(err)
        else
            res.redirect(302, '/settings/profile')
    })  
}

Actually that didn't throw errors but the document in the database hasn't been changed.

My question:

What's the best way to upsert my logged in user?



via kentor

No comments:

Post a Comment