我正在尝试使博客在node.js中仍然是菜鸟,我创建了一个主页,该页面应该为我提供最近的帖子,以获取我要使用Date对象文档上看到的date对象的两种最新方法:我可以尝试过滤模型上的日期 Date.getDate()返回一个数字 Date.now()当前日期也是一个数字
我的模型查询是这样写的
让日期= Date.getDate()-1;
Post.find({postedOn:{$ gt:Date.now,$ lt:date}})。then()。catch()
但是我收到一个错误消息,指出我可以执行任何更好的方法来定义getDate,为什么getDate是未定义的?
答案 0 :(得分:0)
您可以这样做:
let date = new Date();
Post.find({
postedOn: {
$gt: date,
$lt: (date.setDate(date.getDate()-1))
}
})
.then()
.catch()
您也可以使用特定日期,例如:
Post.find({
"postedOn": {
"$gte": new Date(2019, 7, 22),
"$lt": new Date(2017, 7, 21)}
})