保存后猫鼬更新

Mongoose update after save

我遗漏了一些关于 Mongoose 保存功能的回调。 我正在尝试插入新交易,如果成功,则更新用户帐户。这个问题,我相信,这会导致所有人都更新到最后一个人的数量。我想做的是在保存另一个文档后更新一个文档。

这是代码。请让我知道我做错了什么。 提前致谢。

//find all schedules
SchedModel.find({ Day_Of_Week: day }, null, null, function (err, Sched) {
   if (!err) {
      //iterate through what we find
      for (var key in Sched) {
         if (Sched.hasOwnProperty(key)) {
            var val = Sched[key];
            console.log("val : " + val);
            var Sched_Descr = day || ' Sched Trans';  
            var this_trans = new TransModel({
                         mID: val.K_Id,
                         mDate: today,
                         mDescr: Sched_Descr,
                         mAmt: val.mAmt
             });
             //insert the new trans
             this_trans.save(function (err, trans) {
                if (!err) {
                   //when we insert new trans, get the update model
                   MyModel.findById(val.K_Id, function (err, Model) {
                      Model.Balance = Model.Balance + val.mAmt;
                      //save model, this update to the last in the list
                      Model.save(function (err) {
                         if (!err) {
                            console.log("updated");
                         } else {
                            console.log(err);
                         }
                      });
                   });              
                } else {
                   return console.log(err);
                }
             });                           
          }
       }
    } else {
       console.log(err);
    };
 });

更新:ES6 的 let 非常简单地解决了这个问题,只需在您的原始代码中将 var 替换为 let 就可以了。


您的 this_trans 和此类变量在 for-in 循环的每次迭代中都不是唯一的。您可能想将其包装在一个自执行的匿名函数范围内 ((function(){})())

//find all schedules
SchedModel.find({ Day_Of_Week: day }, null, null, function (err, Sched) {
   if (!err) {
      //iterate through what we find
      for (var key in Sched) {
        (function(key){       // self-executing anonymous function scope
         if (Sched.hasOwnProperty(key)) {
            var val = Sched[key];
            console.log("val : " + val);
            var Sched_Descr = day || ' Sched Trans';  
            var this_trans = new TransModel({
                         mID: val.K_Id,
                         mDate: today,
                         mDescr: Sched_Descr,
                         mAmt: val.mAmt
             });
             //insert the new trans
             this_trans.save(function (err, trans) {
                if (!err) {
                   //when we insert new trans, get the update model
                   MyModel.findById(val.K_Id, function (err, Model) {
                      Model.Balance = Model.Balance + val.mAmt;
                      //save model, this update to the last in the list
                      Model.save(function (err) {
                         if (!err) {
                            console.log("updated");
                         } else {
                            console.log(err);
                         }
                      });
                   });              
                } else {
                   return console.log(err);
                }
             });                           
          }
        })(key);
      }
    } else {
       console.log(err);
    };
 });