node.js - NodeJS Can't Access Variable Inside Callback -
i believe problem being async, not know solution.
pagescontroller.buy = function() { var table=""; selling.find({}, function(err, res) { (var in res) { console.log(res[i].addr); table = table + "res[i].addr"; } }); this.table = table; console.log(table); this.render(); }
my issue this.table=table
returning undefined if try access outside of function, , cannot figure out how display table on page.
the problem selling.find asynchronous , isn't complete time this.table = table executed. try following.
pagescontroller.buy = function() { var = this; selling.find({}, function(err, res) { var table = ''; (var in res) { console.log(res[i].addr); table = table + res[i].addr; } that.table = table; console.log(table); that.render(); }); }
that guarantee table isn't used until after results have been fetched , table has been populated.
Comments
Post a Comment