【发布时间】:2018-12-22 22:36:45
【问题描述】:
我昨天发布了这个问题,因为我不知道如何解决我的问题。
Change variable value in document after some time passes?
有人告诉我我需要使用预钩。我试图这样做,但“this”指的是查询,而不是文档。所以我无法检索文件来检查 4 周是否过去了。 (检查问题,你会得到它)
因为我不知道如何让这个 .pre('find') 使用我每个文档中的变量(所以它会检查 4 周是否过去了)我正在考虑遍历所有这些变量并检查是否4 周过去了。
router.get('/judet/:id([0-9]{2})', middleware.access2, function(req, res)
{
var title = "Dashboard";
Somer.find({}, function(err, someri)
{
if(err)
{
console.log(err);
}
else
{
res.render("dashboard", {title: title, id:req.params.id, someri:someri});
}
});
}); ///get route
var someriSchema = new mongoose.Schema({
nume: {type: String, required: true},
dateOfIntroduction: {type:Date, default: Date.now, get: formatareData},
});
someriSchema.pre('find', function(next) {
console.log(this.dateOfIntroduction); <- this will return undefined, because this refers to the query, actually
next();
});///schema and the pre hook. I thought I could use it like this, and inside the body of the pre hook I can check for the date
这就是我要说的:
router.get('/judet/:id([0-9]{2})', middleware.access2, function(req, res)
{
var title = "Dashboard | Best DAVNIC73";
Somer.find({}, function(err, someri)
{
if(err)
{
console.log(err);
}
else
{
someri.forEach(function(somer)
{
///check if 4 weeks passed and then update the deactivate variable
})
res.render("dashboard", {title: title, id:req.params.id, someri:someri});
}
});
});
但我认为如果我的数据库中有很多条目,这将是非常糟糕的性能,我认为这不是最好的方法。
那么,如果我被正确告知并且我应该使用 pre hook 来获取我所说的内容,我怎样才能让它引用文档?
【问题讨论】: