0

I am doing a request on a mongoDB and I would like to increment a variable for each object I found in my database.

My problem is that my variable totalSize seems to not persist the data it gets, I don't know why :/

I believed it was a closure problem in js but someone told me to see if it's not the asynchronous trait of query object that cause my problem.

I'm lost :/

var totalSize = 0;
for (var i = json[0].game.length - 1; i >= 0; i--) {
//When I found a game, I would like to increment his size in totalSize
    Game.findOne({
        'steamID': json[0].game[i].appID[0]
    }, function (err, game) {
        if (err) return handleError(err);
        if (game) {
            //everything is fine here totalSize is the right number
            totalSize += game.size;
        }
    })// where he "forget" my var
    //totalSize is still at 0 like I never incremented the variable
    console.log(totalSize);
}

res.render('user', {
                 steamid: steamID,
                 steamid64: steamID64,
                 size: totalSize,
                 content: json
             });
hyptos
  • 160
  • 1
  • 3
  • 10

1 Answers1

1

findOne is asyncronous, so console.log is executed before findOne is finished

var totalSize = 0;
for (var i = json[0].game.length - 1; i >= 0; i--) {
//When I found a game, I would like to increment his size in totalSize
    Game.findOne({
        'steamID': json[0].game[i].appID[0]
    }, function (err, game) {
        if (err) return handleError(err);
        if (game) {
            //everything is fine here totalSize is the right number
           totalSize += game.size;
        }
        console.log(totalSize);
    })

}

Do it like this:

function findTotalSize(callback){
    var totalSize = 0;
    var gameLength = json[0].game.length;
    for (var i = gameLength - 1; i >= 0; i--) {
        Game.findOne({
            'steamID': json[0].game[i].appID[0]
        }, function (err, game) {
            if (err) return handleError(err);
            if (game) {
               totalSize += game.size;
            }
            if(--gameLength == 0)
               callback(totalSize);
        })
    }
}

//use it
findTotalSize(function(totalSize){
    res.render('user', {
             steamid: steamID,
             steamid64: steamID64,
             size: totalSize,
             content: json
         });
});
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • nope I need the totalSize outside of the function findOne and being updated with the game.size . You just moved it inside and that's not what I want. – hyptos Jun 29 '13 at 09:40
  • see my edit I just need the totalSize being updated for my res.render I don't know if you see what I'm trying to do :/ by the way thanks for you time :) – hyptos Jun 29 '13 at 09:58
  • @hyptos I edited once more :) you should learn more about asynchronous functions: http://stackoverflow.com/questions/6898779/how-to-write-asynchronous-functions-for-node-js – karaxuna Jun 29 '13 at 10:03
  • Thanks first time with nodejs mongo and their friends I will look into the post you told me ! – hyptos Jun 29 '13 at 10:19