sails.js: how to update model sails.js: how to update model angularjs angularjs

sails.js: how to update model


So you figured out your problem, sort of. req.body is already an object. But you really should sanitize it before you put it into your update and then save the object. There's a lot of reasons for this but with Mongo when you get only a partial object you'll replace the object in the collection which, in your example with a user, could be bad. When I send users to the frontend I cull off things I don't want transmitted all over like passwords. The other reason is the golden rule of web application development - never trust the client! I'd start with something like:

var user = User.findOne(req.body.id).done(function(error, user) {    if(error) {        // do something with the error.    }    if(req.body.email) {        // validate whether the email address is valid?        // Then save it to the object.        user.email = req.body.email;    }    // Repeat for each eligible attribute, etc.    user.save(function(error) {        if(error) {            // do something with the error.        } else {            // value saved!            req.send(user);        }    });});