【问题标题】:Dates not updating properly in nodeJSnodeJS中的日期没有正确更新
【发布时间】:2014-09-26 06:17:52
【问题描述】:

以下是我没有正确更新日期的代码-

exports.saveWeekAvailability = function(req, res){
  console.log("-- SAVE WEEK AVAILABILITY --");
  weekData = req.body;
  weekData.sort(function(a, b){ var dateA = new Date(a.currDate); var dateB = new Date(b.currDate); return dateA-dateB;});
  var x=0;
  var resultArr = [];
  for(var i=0; i< weekData.length; i++) {
    (function(i) {                
      Availability.findOne({employee_id:weekData[i].employee_id,currDate:weekData[i].currDate},function(err,response){
        console.log("============= RESPONSE ==============");
        console.log("I: "+i);
        console.log("X: "+x);
        if ( null !== response ) {
          response.is_morning_scheduled = weekData[x].is_morning_scheduled;
          response.is_night_scheduled = weekData[x].is_night_scheduled;
          console.log("-----------IN NULL IN NULL IN NULL---------------");
          console.log(response);
          // CODE TO UPDATE
        }
        else{
          addAvailability(x);
        }
        x++;
      })
    })(i);
  }
};

场景-

对于Tue, 07 Oct 2014 18:30:00 GMTWed, 08 Oct 2014 18:30:00 GMT 的日期,我的早上日程设置为真,例如 - is_morning_scheduled: true

但在 Response 中,您可以查看 true 是否为 currDate: Sat Oct 11 2014 00:00:00 GMT+0530 (IST)currDate: Sat Oct 07 2014 00:00:00 GMT+0530 (IST) 日期。

让我知道我在这个概念上做错了什么。

weekData -

[
  {
    currDate: 'Sat, 04 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: false,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  },
  {
    currDate: 'Sun, 05 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: false,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  },
  {
    currDate: 'Mon, 06 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: false,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  },
  {
    currDate: 'Tue, 07 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: true,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  },
  {
    currDate: 'Wed, 08 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: true,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  },
  {
    currDate: 'Thu, 09 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: false,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  },
  {
    currDate: 'Fri, 10 Oct 2014 18:30:00 GMT',
    is_morning_scheduled: false,
    is_night_scheduled: false,
    employee_id: '53d89f0e5bfa37320be2d1f7',
  }
]

现在在安慰 response 之后,我得到的输出如下 -

============= RESPONSE ==============
I: 0
X: 0
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a556,
  currDate: Sun Oct 05 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: false,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0
}
============= RESPONSE ==============
I: 5
X: 1
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a55b,
  currDate: Fri Oct 10 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: false,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0
}
============= RESPONSE ==============
I: 1
X: 2
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a557,
  currDate: Mon Oct 06 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: false,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0
}
============= RESPONSE ==============
I: 6
X: 3
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a55c,
  currDate: Sat Oct 11 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: true,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0,
}
============= RESPONSE ==============
I: 2
X: 4
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a558,
  currDate: Tue Oct 07 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: true,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0
}
============= RESPONSE ==============
I: 3
X: 5
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a559,
  currDate: Wed Oct 08 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: false,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0
}
============= RESPONSE ==============
I: 4
X: 6
-----------IN NULL IN NULL IN NULL---------------
{
  _id: 5424f0b679e352ff0ce0a55a,
  currDate: Thu Oct 09 2014 00:00:00 GMT+0530 (IST),
  is_morning_scheduled: false,
  is_night_scheduled: false,
  employee_id: 53d89f0e5bfa37320be2d1f7,
  __v: 0
}

【问题讨论】:

    标签: javascript node.js asynchronous callback scope


    【解决方案1】:

    Availability.findOne(... 的调用使用异步回调,因此不能保证以正确的顺序触发,因此您看到i 变量以奇怪的顺序上升(0、5、1、 6, 2, 3, 4) 并且不同于按顺序递增的 x 变量 (0, 1, 2, 3, 4, 5, 6)。

    为什么你甚至有 x 变量,只需使用 i 代替,因为你已经将它包装在一个闭包中,即:

    Availability.findOne({
        employee_id: weekData[i].employee_id,
        currDate: weekData[i].currDate
    },
    function(err, response) {
        console.log("============= RESPONSE ==============");
        console.log("I: " + i);
    
        if (null !== response) {
            response.is_morning_scheduled = weekData[i].is_morning_scheduled;
            response.is_night_scheduled = weekData[i].is_night_scheduled;
    
            console.log("-----------IN NULL IN NULL IN NULL---------------");
            console.log(response);
            // CODE TO UPDATE
        }
        else {
            addAvailability(i);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 2011-09-27
      相关资源
      最近更新 更多