lundi 26 février 2018

Find dates after today [Expess + mongoose]

I building a web application that lists events in chronological order and i only want to list future events from a specific date, however my mongoose queries keep returning and empty object. If i query all events, my code works fine, but when ever i add filters, it always returns an empty object.

my model:

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"
} });

my route

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;
        console.log(event.date.getMonth())
    });
    res.render('events', {
        events: events,
    })
}) });

mongodb object

{
"_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 }




Aucun commentaire:

Enregistrer un commentaire