Not working with method "UPDATE" in API REST MEAN + Mongoose Not working with method "UPDATE" in API REST MEAN + Mongoose mongoose mongoose

Not working with method "UPDATE" in API REST MEAN + Mongoose


find return an Array (list of documents) . you can't do save on an array object. Instead try findOne if your email field is unique.

findOne returns a single document, so you can save that.

Replace

Usuario.find({ email: req.params.email }, function(err, usuario)

with :

Usuario.findOne({ email: req.params.email }, function(err, usuario)


To update a model it's much easier to use the findOneAndUpdate() update API. The method finds a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. The query executes immediately if callback is passed.

The syntax is:

Model#findOneAndUpdate([conditions], [doc], [options], [callback])

Parameters:

[conditions] <Object> - the query to match[doc] <Object> - the document to update[options] <Object> - update options[callback] <Function> - callback

For example, the above function can be re-written to use the findOneAndUpdate() method as:

exports.updateUsuarioByEmail = function (req, res) {    console.log('updateUsuarioByEmail');    console.log("PARAM ID" + req.params.email);    var doc = {},        conditions = { "email": req.params.email },        options = { "new": true };    if (req.body.email != null) doc.email = req.body.email;    if (req.body.password != null) doc.password = req.body.password;    if (req.body.listaCardsSorting != null)        doc.listaCardsSorting = req.body.listaCardsSorting;    Usuario.findOneAndUpdate(        conditions,        doc,        options,        function(err, usuario) {            if(!err) {                console.log('Updated usuario');                return res.send({                     status: 'OK',                     usuario: usuario                 });            } else {                if(err.name == 'ValidationError') {                    res.statusCode = 400;                    res.send({ error: 'Validation error' });                } else {                    res.statusCode = 500;                    res.send({ error: 'Server error' });                }                console.log('Internal error(%d): %s',res.statusCode,err.message);            }                               }    )    };


Here remained the solution:

exports.updateUsuarioByEmail = function (req, res) {console.log('updateUsuarioByEmail');return Usuario.findOne({email: req.params.email}, function (err, usuario) {    usuario.email = req.body.email || usuario.email;    usuario.password = req.body.password || usuario.password;    usuario.listaCardsSorting = req.body.listaCardsSorting || usuario.listaCardsSorting;    return usuario.save(function (err) {        if (!err) {            console.log('Updated');            return res.send({status: 'OK', usuario: usuario});        } else {            if (err.name == 'ValidationError') {                res.statusCode = 400;                res.send({error: 'Validation error'});            } else {                res.statusCode = 500;                res.send({error: 'Server error'});            }            console.log('Internal error(%d): %s', res.statusCode, err.message);        }        res.send(usuario);    });});

};