var schema = new Schema({...}, {
timestamps: true,
id: false,
toJSON: {
virtuals: true,
},
toObject: {
virtual: true,
}
});
schema.virtual('updated').get(function() {
if(typeof this.updatedAt === "undefined" && typeof this.createdAt === "undefined") return "";
var updated = (typeof this.updatedAt === "undefined") ? this.createdAt : this.updatedAt;
return "Updated "+moment(updated).fromNow();
});
此代码为最近的工作 - 为某个特定的实例updatedAt出现在8月24日,但任何新的编辑文档不更新时间戳。
感觉就像我在这里错过了一些非常愚蠢的东西。
你能检查type.of this.updatedAt吗? – abdulbarik
@abdulbarik typeof league.updatedAt => object –
我复制粘贴你的代码并运行在我的服务器上,它与猫鼬4.6.1一起工作得很好,所以你可能错过了别的地方。 请提及您正在使用的猫鼬版本,或任何猫鼬插件。 –