我想使用.map()
函数转换对象,我尝试获取订单表的所有订单,我有另一个名为OrderProducts
的表,在此table我有一些产品,每个产品都有一个OrderId
,我想在订单对象中获得每个产品的每个订单...
我的代码不起作用(如果我删除了_.forEach
功能,它就有效):
self.ecOrderDao.getAll().success( function(allOrders){
_.forEach(allOrders, function(order) {
self.ecOrderProductDao.getAllByOrder(order.id).success( function(orderProducts){
async.map(orders, function(order, callback){
order.products = orderProducts;
callback(null, order);
}, function(err, transformed){
response.render('ecorder/_index', {allOrders: transformed});
});
});
});
});

答案 0 :(得分:1)
这是更正后的代码;
self.ecOrderDao.getAll().success( function(allOrders){
async.map(allOrders,
function(order, callback){
self.ecOrderProductDao.getAllByOrder(order.id).success( function(orderProducts){
order.products = orderProducts;
callback(null, order);
});
},
function(err, transformed){
response.render('ecorder/_index', {allOrders: transformed});
});
});