Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodeJS中的日期更新不正确_Javascript_Node.js_Asynchronous_Callback_Scope - Fatal编程技术网

Javascript nodeJS中的日期更新不正确

Javascript nodeJS中的日期更新不正确,javascript,node.js,asynchronous,callback,scope,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

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

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);
  }
};
现在,在安慰
响应之后,我得到了如下输出-

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

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