2015-03-03 27 views
0

我想使用.map()函數來轉換一個對象,我嘗試獲取我訂單的所有訂單的表,我有另一個表叫做OrderProducts,在這個表中我有一些產品,每個產品都有一個OrderId,我想獲得每個訂單與訂單的對象內的每個產品...在NodeJS中使用map()和forEach

我的代碼不能正常工作(它的工作原理,如果我刪除_.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}); 
 
     }); 
 
    }); 
 
    }); 
 
});

回答

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}); 
     }); 
}); 
+0

謝謝你的工作;) – tonymx227 2015-03-05 13:42:50