javascript - 下划线和 Mongoose : Use document as outer function's return value

标签 javascript node.js mongodb mongoose underscore.js

我正在尝试将 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/

相关文章:

Javascript在对象数组中查找具有匹配属性的对象,并获取另一个属性(如果存在)

javascript - embed.fields[1].value : Must be 1024 or fewer in length. embed.fields[2].value: 长度必须为 1024 或更少

ruby-on-rails - Mongoid 默认范围覆盖默认值。为什么?

python - 在 pymongo 中搜索时忽略一个特定或任何特殊字符

php - 排序在新的 mongodb PECL 扩展中是如何工作的?

javascript - AngularJS NG重复: limitTo not limiting object loop

javascript - 表单提交不遵循重定向 (Node.js)

javascript - JS时钟问题,以onclick启动

node.js - 如何在 Mongoose 中构建动态查询?

javascript - 如何发送数据正确的axios Error : Multipart: Boundary not found