2017-02-25 8 views
0

私は、24-irdsと3-smallpartsを作成したいと思いますが、代わりに24-irdsと72-小さい部分。 smallparts forEachのように、小部分の代わりに鳥の数を繰り返すようです。なぜどんなアイデア?私が把握するために管理し、事前forEachステートメントが予想より多く実行されている理由

exports.pickup = function (req, res) { 
    async.waterfall([ 
    function (callback) { 
     var order = createOrder(req); 
     callback(null, order); 
    }, 
    function (order, callback) { 
     if (req.body.irds.length > 0) { 
     _(req.body.irds).forEach(function (n) { 
      var receiver = new Receiver(n); 
      receiver.order = order._id; 
      receiver.company = req.user.company; 
      receiver.user = req.user; 
      receiver.date = req.body.date; 
      receiver.location = req.user.location; 
      order.receivers.push(receiver._id); 
      receiver.save(function (err) { 
      callback(null, order); 
      if (err) { 
       console.log('error receiver exists'); 
      } 
      }); 
     }); 
     } else { 
     callback(null, order); 
     } 
    }, 
    function (order, callback) { 
     if (req.body.smallParts.length > 0) { 
     _(req.body.smallParts).forEach(function (n) { 
      var now = new Date(); 
      var query1 = {'_id': req.user.company, 'products.product': n.product}; 
      var query2 = {'_id': req.user.company}; 
      var update1 = { 
      $inc: { 
       'products.$.quantity': n.quantityRequested, 
       'products.$.quantityOnhand': n.quantityRequested 
      }, 
      'products.$.updated': now, 
      'products.$.lastPickUp.date': now, 
      'products.$.lastPickUp.quantity': n.quantityRequested 
      }; 
      var update2 = { 
      $push: { 
       'products': { 
       'product': n.product, 
       'quantity': n.quantityRequested, 
       'quantityOnhand': n.quantityRequested, 
       'updated': now, 
       'lastPickUp.date': now, 
       'lastPickUp.quantity': n.quantityRequested 
       } 
      } 
      }; 

      var options = {upsert: true}; 

      Companies.findOneAndUpdate(query1, update1, function (err, doc) { 
      if (!doc) { 
       Companies.findOneAndUpdate(query2, update2, function (err, doc) { 
       if (err) { 
        throw err; 
       } 
       }); 
      } 
      }); 

      //save smallparts 
      n._id = new ObjectId(); 
      var smallPart = new SmallPart(n); 
      smallPart.order = order._id; 
      smallPart.quantity = n.quantityRequested; 
      smallPart.company = req.user.company; 
      smallPart.user = req.user; 
      smallPart.location = req.user.location; 
      smallPart.date = req.body.date; 
      order.smallParts.push(smallPart._id); 
      smallPart.save(function (err) { 
      callback(null, order); 
      if (err) { 
       console.log(err); 
      } 
      }); 
     }) 
     } else { 
     callback(null, order) 
     } 
    }, 
    function (order, callback) { 
     order.location = req.user.location; 
     order.company = req.user.company; 
     order.save(function (err) { 
     callback(null, 'done'); 
     if (err) { 
      console.log(err); 
     } 
     }); 
    } 
    ], function (err) { 
    if (!err) { 
     res.status(200).json(); 
    } else { 
     console.log(err); 
    } 
    }); 
}; 

答えて

0

感謝。

exports.pickup = function (req, res) { 

    var order = createOrder(req); 
    order.location = req.user.location; 
    order.company = req.user.company; 
    order.type = 'pickup'; 

    async.series([ 
     function (callback) { 
     if (req.body.irds.length > 0) { 
      _(req.body.irds).forEach(function (n) { 
      var receiver = new Receiver(n); 
      receiver.order = order._id; 
      receiver.company = req.user.company; 
      receiver.user = req.user; 
      receiver.date = req.body.date; 
      receiver.location = req.user.location; 
      order.receivers.push(receiver._id); 
      receiver.save(function (err) { 
       if (err) { 
       console.log('error saving receiver'); 
       } 
      }); 
      }); 
     } 
     callback(null); 
     }, 
     function (callback) { 
     if (req.body.smallParts.length > 0) { 
      _(req.body.smallParts).forEach(function (n) { 
      var now = new Date(); 
      var query1 = {'_id': req.user.company, 'products.product': n.product}; 
      var query2 = {'_id': req.user.company}; 
      var update1 = { 
       $inc: { 
       'products.$.quantity': n.quantityRequested, 
       'products.$.quantityOnhand': n.quantityRequested 
       }, 
       'products.$.updated': now, 
       'products.$.lastPickUp.date': now, 
       'products.$.lastPickUp.quantity': n.quantityRequested 
      }; 
      var update2 = { 
       $push: { 
       'products': { 
        'product': n.product, 
        'quantity': n.quantityRequested, 
        'quantityOnhand': n.quantityRequested, 
        'updated': now, 
        'lastPickUp.date': now, 
        'lastPickUp.quantity': n.quantityRequested 
       } 
       } 
      }; 

      var options = {upsert: true}; 

      Companies.findOneAndUpdate(query1, update1, function (err, doc) { 
       if (!doc) { 
       Companies.findOneAndUpdate(query2, update2, function (err, doc) { 
        if (err) { 
        throw err; 
        } 
       }); 
       } 
      }); 

      //save smallparts 
      n._id = new ObjectId(); 
      var smallPart = new SmallPart(n); 
      smallPart.order = order._id; 
      smallPart.quantity = n.quantityRequested; 
      smallPart.company = req.user.company; 
      smallPart.user = req.user; 
      smallPart.location = req.user.location; 
      smallPart.date = req.body.date; 
      order.smallParts.push(smallPart._id); 
      smallPart.save(function (err) { 
       // callback(null, order); 
       if (err) { 
       console.log(err); 
       } 
      }); 
      }) 
     } 
     callback(null, order) 
     } 
    ], 
    function (err) { 
     if (!err) { 
     order.save(function (err) { 
      if (!err) { 
      res.status(200).json(); 
      } else { 
      console.log('error saving order') 
      } 
     }); 
     } else { 
     console.log(err); 
     } 
    }); 

}; 
関連する問題