我试图在循环中更新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
保持不变。如何在将其发送到渲染之前更新它,以便用户可以看到人的时间而不是印章?
UPDATE 计时器数组的片段
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 works
。但是指定nextTick
仍然没有。
答案 0 :(得分:1)
您可以使用.lean()
使返回的文档成为普通的javascript对象。这样你就可以随意操作它。
var queryPromise = cronie.find({}).
where('id').equals(req.user._id).
lean(). // make returned document be a plain js object
exec(); //mongoose promise
启用了
lean
选项的查询返回的文档是纯JavaScript对象,而不是MongooseDocuments。 他们没有save
方法,getter / setter或其他Mongoose魔法应用。