Following is my code which is not properly updating the dates-
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);
}
};
Scenario -
For dates Tue, 07 Oct 2014 18:30:00 GMT & Wed, 08 Oct 2014 18:30:00 GMT my morning schedule is set true like - is_morning_scheduled: true.
But in Response you can check it is taking true for currDate: Sat Oct 11 2014 00:00:00 GMT+0530 (IST) & currDate: Sat Oct 07 2014 00:00:00 GMT+0530 (IST) date.
Let me know what I am doing wrong here with the concept.
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',
}
]
Now after consoling the response I am getting the output like -
============= 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
}
The call to Availability.findOne(... uses an async callback and is thus not guaranteed to fire in the correct order, hence the reason you're seeing the i variable go up in a weird order (0, 5, 1, 6, 2, 3, 4) and is different from the x variable which increments in order (0, 1, 2, 3, 4, 5, 6).
Why do you even have the x variable, just use i instead since you're already wrapping it in a closure i.e:
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);
}
});