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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -