很简单,可以从回调内部填充对象符号吗?
被诅咒的 javascript 闭包!
var symbols = {};
markets.find(function(err, markets) {
for(var market in markets) {
symbols[markets[market].symbol] = markets[market].label;
}
});
console.log(symbols);
很简单,可以从回调内部填充对象符号吗?
被诅咒的 javascript 闭包!
var symbols = {};
markets.find(function(err, markets) {
for(var market in markets) {
symbols[markets[market].symbol] = markets[market].label;
}
});
console.log(symbols);
async/waterfall是解决这个问题的正确方法。由于 .find() 是异步的,你也可以做一个 forEach :
markets.forEach(function(m) {
symbols[m.symbol] = m.label;
});