日期未在nodeJS中正确更新

时间:2014-09-26 06:17:52

标签: javascript node.js asynchronous callback scope

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

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 GMT&amp; Wed, 08 Oct 2014 18:30:00 GMT我的早晨时间表设置为真如is_morning_scheduled: true

但在响应中,您可以检查true是否currDate: Sat Oct 11 2014 00:00:00 GMT+0530 (IST)&amp; 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之后,我得到了像< - p>这样的输出

============= 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
}

1 个答案:

答案 0 :(得分:2)

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