Mongoose update not working

I checked many questions on stackoverflow and changed this simple query that doesn't work. I do not know why...

 router.get('/users/:username/suspend', function(req, res){
        var username = req.params.username;
        User.findByUsername(username, function(err, user){
            console.log(user);
            user.suspended = true;
            user.save(function(err, u){
                res.redirect('ok');
            });

        });

});

      

I have tried many ways like using model.update with upsert true and false .... Also its console.log gives the correct username in the callback .... but there is no change in the database ...

+3


source to share


2 answers


If you are running mongoose version 3.x before .save can be used to update the model, you need to mark this field as changed on the model.



 router.get('/users/:username/suspend', function(req, res){
    var username = req.params.username;
    User.findByUsername(username, function(err, user){
        console.log(user);
        user.suspended = true;
        user.markModified('suspended');
        user.save(function(err, u){
            res.redirect('ok');
        });

    });

});

      

0


source


Another thing:



user.set('suspended', true);

      

0


source







All Articles