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

Solution

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

Discussion

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教程

代码区博客精选文章
分页:12
转载请注明
本文标题:nodejs and mongodb (mongojs): Trying to query and update database within a for l ...
本站链接:https://www.codesec.net/view/610882.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(60)