gpt4 book ai didi

javascript - 更新循环内的变量

转载 作者:行者123 更新时间:2023-12-03 01:49:46 30 4
gpt4 key购买 nike

我正在尝试在循环内更新 var,然后返回到 promise 。但 var 无法更新。到目前为止我已经尝试过了,

var queryPromise = cronie.find({}).
where('id').equals(req.user._id).
exec(); //mongoose promise


queryPromise.then(function(timer) {
for (var i = 0, len = timer.length; i < len; i++) {
x = moment.unix(timer[i].nextTick).format('dddd, MMMM Do, YYYY h:mm:ss A'); //convert to human;
console.log(x);
timer[i].nextTick = x;
console.log(timer[i].nextTick);
}

return timer;
}).then(function(timer) {
res.render('dash.ejs', {
user: req.user,
timer: timer
});

});
});

这是输出:

0|server   | Monday, May 21st, 2018 11:00:00 PM
0|server | 1526943600
0|server | Monday, May 21st, 2018 7:28:22 PM
0|server | 1526930902
0|server | Monday, May 21st, 2018 7:28:22 PM
0|server | 1526930902
0|server | Monday, May 21st, 2018 7:28:22 PM
0|server | 1526930902
0|server | Monday, May 21st, 2018 7:28:22 PM
0|server | 1526930902

但是timer[i].nextTick保持不变。在将其发送到渲染之前如何更新它,以便用户可以看到人类时间而不是时间戳?

更新计时器数组的切片

0|server   |   { _id: 5b031dd6c0d99f79946f263f,
0|server | id: '5b031dd5c0d99f79946f263a',
0|server | lastTick: 1526930902,
0|server | nextTick: 1526930902,
0|server | lastUpdate: 1526930902,
0|server | __v: 0,
0|server | timerStatus: false,
0|server | emailStatus: false }

更新2

添加

timer[i].date =  moment.unix(timer[i].nextTick).format('dddd, MMMM Do, YYYY h:mm:ss A');

然后将 ejs 更改为 timer.date 有效。但分配 nextTick 仍然不行。

最佳答案

您可以使用.lean()使返回的文档成为一个普通的 javascript 对象。这样您以后就可以随意操纵它。

var queryPromise = cronie.find({}).
where('id').equals(req.user._id).
lean(). // make returned document be a plain js object
exec(); //mongoose promise

Documents returned from queries with the lean option enabled are plain javascript objects, not MongooseDocuments. They have no save method, getters/setters or other Mongoose magic applied.

关于javascript - 更新循环内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50473288/

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