【发布时间】:2017-01-22 13:38:50
【问题描述】:
架构:
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 日,但是对文档的任何新编辑不会更新时间戳。 p>
感觉我在这里遗漏了一些非常愚蠢的东西。
【问题讨论】:
-
你能检查一下 this.updatedAt 的类型吗?
-
@abdulbarik typeof League.updatedAt => 对象
-
我复制粘贴了您的代码并在我的服务器上运行,它在 mongoose 4.6.1 上运行得非常好,所以您可能在其他地方遗漏了一些东西。请提及您正在使用的猫鼬版本,或任何猫鼬插件。
-
@PuneetSingh Mongoose v.4.5.3。进一步测试表明 createdAt 创建成功并且是正确的,但是如果我们随后更新文档,则 updatedAt 的值不会改变。这是我调用编辑的地方:github.com/simon--poole/EventVODS/blob/master/app/routes/api/…
-
看起来这是猫鼬时间戳中的一个已知问题。修复是在 3 天前完成的。也许您可以将 Mongoose 时间戳更新到最新版本并尝试一下?此链接中提供了有关修复的更多信息。 github.com/drudge/mongoose-timestamp/pull/37/commits/…
标签: node.js mongodb mongoose-schema