【发布时间】:2018-02-27 23:26:50
【问题描述】:
我构建了一个按时间顺序列出事件的 Web 应用程序,我只想列出特定日期的未来事件,但是我的 mongoose 查询不断返回并且为空对象。如果我查询所有事件,我的代码可以正常工作,但是当我添加过滤器时,它总是返回一个空对象。
我的模特:
const EventSchema = new Schema({
name: {
type: String,
required: true
},
description: {
type: String,
required: true
},
address: {
type: String,
required: true
},
date: {
type: Date,
required: true
},
start: {
type: String,
required: true
},
end: {
type: String,
required: true
},
url: {
type: String,
required: true
},
phone: {
type: String,
required: true
},
email: {
type: String,
required: true
},
img: {
type: String,
default: "noimage.jpg"
} });
我的路线
app.get('/events', (req, res) => {
Event.find({ date: { $gte: new Date(2018, 01, 01) } }).exec(function(err, events) {
console.log(events)
events.forEach(function(event) {
const month = event.date.getMonth() + 1;
const day = event.date.getDate();
const year = event.date.getYear();
event.formatDate = month + '/' + day + '/' + year;
});
res.render('events', {
events: events,
})
}) });
mongodb 示例对象(这不是唯一的)
{
"_id": {
"$oid": "5a736236ee687920e2fb736f"
},
"img": "hauntedhouse.jpg",
"name": "Holloween Party",
"date": "2017-10-31",
"description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Praesentium hic molestiae cum eaque? Officia, temporibus, nobis! In labore quod, esse alias, quos, ex quae eius enim quisquam autem fugit consectetur.",
"address": "Lorenzo \u201cLore\u201d Garcia Park - 413 E. Clark Ave",
"start": "6:00 PM",
"end": "10:00 PM",
"phone": "555-555-555",
"email": "asdf@asdf.com",
"url": "http://www.test.com",
"__v": 0 }
【问题讨论】:
-
你知道在javascript中日期
months是从零开始的吗?所以01是二月。 -
@ema 是的,我知道这一点,但感谢您让我知道。现在,检索正确的数据是我的首要任务
标签: node.js express web mongoose