简体   繁体   English

我如何在node-orm2的每个函数中执行一些异步操作?

[英]how could I do some async thing in node-orm2's each function?

I have some code like this: 我有一些这样的代码:

req.models.order.find({})
  .each(function(order){
    req.models.food.find({orderId: order.id}{
      //...
      order.foods = ['...'];
      return order;
    })
  })
  .get(function(order){
    //the orders have no 'foods' attribute
  })

How could I fix it? 我该如何解决?

First of all, you can use associations (like one-to-many or many-to-many), which would be the most preferable path to go. 首先,您可以使用关联 (例如一对多或多对多),这是最好的选择。

The other way to do it (maybe not too pro, but surely works) is to make a counter initialized with number of returned orders, decrementing with every next "each" iteration, and eventually firing "get" inside the "each" when the counter reaches 0. 另一种方法(也许不太专业,但肯定可以工作)是使计数器初始化为返回的订单数,然后在每次下一次“每次”迭代时递减,并最终在“每次”内部触发“获取”。计数器达到0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Async库的“Each”函数中定义回调? - How do I define the callback in the Async library's “Each” function? node.js async.each回调,我怎么知道它什么时候完成? - node.js async.each callback, how do I know when it's done? 我怎么能对那个 function 使用异步/等待? - how could I use async/await for that function? 如何使用Babel 7的插件提议装饰器和异步函数来装饰异步方法? - How do I decorate an async method using Babel 7's plugin-proposal-decorators with an async function? 如何对JS上的所有类做一些事情 - How to do some thing to all class on js 如何创建一个 javascript 助手 function / class 可以在每个公开方法之前执行异步任务 - How do I create a javascript helper function / class that can perform an async task before each exposed method 如何使我的节点应用程序同时执行数千次异步操作? - How do I get my node app to perform 1000s of async operations at the same time? 如何在node.js中等待N个异步函数完成,以便对它们的所有组合结果进行一些工作? - How to wait for N number of async functions to finish in node.js so that I can do some work on all their combined results? 如何修复此异步/等待 function? - How do I fix this async/await function? 是否可以对某些事物进行(new Thing())()? - Is it possible to do (new Thing())() for some Thing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM