[英]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.