gpt4 book ai didi

javascript - NodeJS 中的日期未正确更新

转载 作者:行者123 更新时间:2023-12-02 16:57:34 26 4
gpt4 key购买 nike

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

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

场景 -

对于日期星期二,2014年10月7日18:30:00 GMT星期三,2014年10月8日18:30:00 GMT我早上的时间表设置为true - is_morning_scheduled:true

但在 Response 中,您可以检查 currDate: Sat Oct 11 2014 00:00:00 GMT+0530 (IST) 是否采用 true code> & currDate:2014 年 10 月 7 日星期六 00:00:00 GMT+0530 (IST) 日期。

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

周数据 -

[
{
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 ==============
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);
}
});

关于javascript - NodeJS 中的日期未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com