mongoose updating a field in a MongoDB not working

Posted by Masiar on Stack Overflow See other posts from Stack Overflow or by Masiar
Published on 2011-11-21T21:27:58Z Indexed on 2011/11/22 1:50 UTC
Read the original article Hit count: 223

Filed under:
|
|

I have this code

var UserSchema = new Schema({
    Username: {type: String, index: true},
    Password: String,
    Email: String,
    Points: {type: Number, default: 0}
});

[...]

    var User = db.model('User');
   /*
    * Function to save the points in the user's account
    */
    function savePoints(name, points){
        if(name != "unregistered user"){
        User.find({Username: name}, function(err, users){

            var oldPoints = users[0].Points;
            var newPoints = oldPoints + points;

            User.update({name: name}, { $inc: {Points: newPoints}}, function(err){
                if(err){
                    console.log("some error happened when update");
                }
                else{
                    console.log("update successfull! with name = " + name);
                    User.find({Username: name}, function(err, users) { 
                        console.log("updated : " + users[0].Points);
                    });
                }
            });

        });
    }
}

savePoints("Masiar", 666);

I would like to update my user (by finding it with its name) by updating his/her points. I'm sure oldPoints and points contain a value, but still my user keep being at zero points. The console prints "update successful".

What am I doing wrong? Sorry for the stupid / noob question.

Masiar

© Stack Overflow or respective owner

Related posts about mongodb

Related posts about node.js