我有一个用mongoDB中的数据填充的表,我想使用moment.js来指示表中任何条目编辑和更新的最后日期/时间。
我的问题是moment.js似乎实时显示日期/时间,而不仅仅是当数据库条目被编辑时。
如何让它显示正确的日期和时间?
Mongoose Schema,其中我为updatedAt
功能使用时间戳:
var environmentSchema = new mongoose.Schema({
ePIMS: String,
codeVersion: String,
region: String
}, {
timestamps: true
});
我尝试使用moment.js 的HTML
<table>
<%= moment(environments.updatedAt) %> <!-- using it here causes the date/time to update in real time -->
<% environments.forEach(function(environment){ %>
<!-- if I use the moment.js code here, it shows it 3 times for all three of the ```td```'s below -->
<tr>
<td><%= environment.ePIMS %></td>
<td><%= environment.codeVersion %></td>
<td><%= environment.region %></td>
</tr>
<% }); %>
</table>
顺便说一句,当我把moment.js代码放在调用mongo集合(environments
(的forEach循环中时,它就可以工作了,但它会显示数据库中每个条目的日期和时间,并单独更新这些日期/时间。有没有办法这样做,但所有条目只有一个日期/时间?
- Environments是一个环境对象数组,您正在尝试更新其中的一个
- 您需要获取一个环境对象并获取该对象的UpdatedAt
- 因此,您可以将[index]大部分放在[0]
environments[0].updatedAt
中,以获得第一个环境的updatedAt - 要获得最后一个,请先使用
sort({"updatedAt": -1})
,然后使用environments[0].updatedAt