I'm writing a multiplayer game(mongojs, nodejs) and trying to figure out how to update user stats based on the outcome of the game. I already have the code written to compute all the post game stats. The problem comes when I try to update the users' stats in a for loop. Here's what i got:

//Game Stats var tempgame = { gameid: 1234, stats: [ { score: 25, user: 'user1' }, { score: 25, user: 'user2' } ] } for(i = 0; i < tempgame.stats.length; i++){ db.users.find({ username: tempgame.stats[i].user }, function(err, res){ if( err != null){ //handle errors here. } else { var userstats = res[0].stats; if( tempgame.stats[i].score > userstats.bestscore ){ //this is where it chokes userstats.bestscore = tempgame.stats[i].score; } //code here to pass back new manipulated stats } }); }

Everything works fine until i try to use the tempgame object within the callback function. It says "cannot read property 'score' of undefined". Is this just a scoping issue?

Also i was thinking it could be an issue with the callback function itself. Maybe the loop would increment before the callback is even run. But even in that case, the score should be be there it would just be pulling from the wrong array index... that's what lead me to believe it may just be a scope issue.

Any help would be greatly appreciated.

Problem courtesy of: v3xx3d


You've been tripped up by the notorious " defining functions inside a loop " problem.

Use "forEach" instead:

tempgame.stats.forEach(function (stat) { db.users.find({ username: stat.user }, function(err, res){ if( err != null){ //handle errors here. } else { var userstats = res[0].stats; if( stat.score > userstats.bestscore ){ //this is where it chokes userstats.bestscore = stat.score; } //code here to pass back new manipulated stats } }); });

Solution courtesy of: mjhm


In addition to the above, if you want to return results back to the application, http://nodeblog.tumblr.com/post/60922749945/nodejs-async-db-query-inside-for-loop

Discussion courtesy of: Avinash Ega

Part of your problem is as mjhm stated in his answer to your question, and is as you have suspected. The i variable is changing before the callback is invoked.

The other half of your problem is because your database calls have not returned yet. Due to the asynchronous nature of NodeJS, your loop will finish before your database calls complete. Additionally, your database calls are not necessarily coming back in the same order you called them. What you need is some sort of flow control like async.js . Using async.map will allow you to make all calls to the DB in parallel and return them as an array of values you can use, after all db calls have been completed.

async.map(tempgame.stats, function(stat, callback){ db.users.find({ username: stat.user }, function(err, res){ if( err != null){ callback(err); } else { callback(null, res[0].stats); } }); }, function(err, stats){ if(err){ //handle errors } else{ stats.forEach(function(stat){ //do something with your array of stats //this wont be called until all database calls have been completed }); } });

Discussion courtesy of: Russ Bradberry

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

本文标题:nodejs and mongodb (mongojs): Trying to query and update database within a for l ...

技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(60)