Underscore 和 Mongoose:使用文档作为外部函数的返回值

发布于 2024-12-13 12:29:26 字数 345 浏览 2 评论 0原文

我正在尝试将 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?
    });
});

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

I'm trying to use Mongoose and Underscore together, to do something like this:

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

Is there any way to do this? I realize I may be trying to force a synchronous paradigm on a library designed to be used asynchronously.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-12-20 12:29:26

您获得价值的唯一方法是通过回调:

检查我的答案:
如何检索类中变量的值< /a>

代码中的重要位置是:

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

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

only way you can get value is by callback:

check my answer at:
How to retrieve value of a variable in class

important places in code are:

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

and

 getActualId: function(callback){
       this.find({where: {actual: 1}}).on('success', function(placeTmp){
       callback(placeTmp['id']);
 })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文