为什么forEach语句运行的次数比预期的多

时间:2017-02-25 20:40:12

标签: mongodb asynchronous lodash

我有以下功能,但是没有按预期工作,例如我想创建24-irds和3-smallparts,而是我得到24-Sirds和72-smallparts。看起来每个小部件都是its而不是小部件的数量。有什么想法吗?

提前致谢

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

1 个答案:

答案 0 :(得分: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);
      }
    });

};