h00*_*00j 3 mongoose node.js locomotivejs
我相信这是异步的问题,但我不知道解决方案.
PagesController.buy = function() {
var table="";
Selling.find({}, function(err, res) {
for (var i in res) {
console.log(res[i].addr);
table = table + "res[i].addr";
}
});
this.table = table;
console.log(table);
this.render();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,this.table=table如果我尝试在函数外部访问它,则返回undefined,我无法弄清楚如何在页面上显示表格.
问题是Selling.find是异步的,并且在执行this.table = table时可能还没有完成.尝试以下内容.
PagesController.buy = function() {
var that = this;
Selling.find({}, function(err, res) {
var table = '';
for (var i in res) {
console.log(res[i].addr);
table = table + res[i].addr;
}
that.table = table;
console.log(table);
that.render();
});
}
Run Code Online (Sandbox Code Playgroud)
这将保证在获取结果并填充表之后才会使用该表.
| 归档时间: |
|
| 查看次数: |
3770 次 |
| 最近记录: |