Sunday, 4 June 2017

Mongoose update found document

I want to update a returned document from a find query, but even though I don't get any errors with the code below it doesn't work as intended:

var projectId = req.params.projectId
var object = req.body
var updatedProject = sanitizeProjectObject(object)

Project.findOne({project_id: projectId}, function(err, project) {
    if(err)
        next(err)

    if(!project)
        next(new Error(404, "Couldn't find a project with this id"))

    if(!isAllowedToEditProject(req, project))
        next(new Error(403, "You are not allowed to edit this project"))

    project.update({ $set: updatedProject }, function(err, resultProject) {
        logger.info(resultProject)
    })
})

Output is:

3:32:07 AM - info: ok=0, n=0, nModified=0

The updatedProject object contains 5 of the document's 8 fields which have changed. I can confirm that the values for these fields in "updatedProject" are different than the stored values.

So how can I update a returned document from the database using mongoose?



via kentor

No comments:

Post a Comment