我正在尝试将 Mongoose 和 Underscore 一起使用,来做这样的事情:
var person_ids = [1, 2, 3];
var persons = _(person_ids).map(function(id) {
Person.findById(id, function(person) { // Non-blocking
// How do I use 'person' as the outer function's return value?
});
});
有什么办法可以做到这一点吗?我意识到我可能正在尝试在设计为异步使用的库上强制采用同步范例。
最佳答案
获得值(value)的唯一方法是通过回调:
查看我的答案: How to retrieve value of a variable in class
代码中的重要位置是:
Place.getActualId(function(id){ console.log(id); });
和
getActualId: function(callback){
this.find({where: {actual: 1}}).on('success', function(placeTmp){
callback(placeTmp['id']);
})
关于javascript - 下划线和 Mongoose : Use document as outer function's return value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7962086/