下划线和猫鼬:使用文档作为外部函数的返回值

Underscore and Mongoose: Use document as outer function's return value

本文关键字:外部 函数 返回值 文档 下划线      更新时间:2023-09-26

我正在尝试使用Mongoose和下划线一起,做这样的事情:

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?
    });
});

有什么办法可以做到吗?我意识到我可能试图在设计为异步使用的库上强制使用同步范型。

获取值的唯一方法是通过回调:

检查我的答案:如何检索类

中变量的值

代码中重要的位置是:

Place.getActualId(function(id){ console.log(id); });

 getActualId: function(callback){
       this.find({where: {actual: 1}}).on('success', function(placeTmp){
       callback(placeTmp['id']);
 })