简体   繁体   中英

Meteor: How to add and query Dates in Mongodb with Momentjs

I want to show all objects which are due today. Everything works properly with my fixture data. But newly added objects won't show up.

Fixture

...
var today = moment().toDate();

    Cards.insert({
        deckName: javascriptId,
        due: today,
        front: 'blaablaa',
        back: 'blaablaablaa'
    });
...

Show objects

Template.showCards.helpers({
    cards: function () {
        var end = moment().toDate();

        return Cards.findOne({
            deckName: this._id,
            due: {"$lte": end}
        });
    }

Add new due date to hide an object

    Template.showCards.events({
    ...
        'click #difficulty button': function(event) {
        var incBy = parseInt(event.target.value);
        console.log(incBy);
        console.log(typeof incBy);
        var today = moment();
        var newDue = moment(today).add(incBy,'days').toDate();
        Cards.update(
            this._id, {
                $set: {due: newDue}
                }
            );
    }
});

Add new objects

Template.addCards.events({
    'submit form': function (event, template) {
        event.preventDefault();
        var deckNameVar = event.target.deckName.value;
        var frontVar = event.target.front.value;
        var backVar = event.target.back.value;
        var today = moment().toDate();
        Cards.insert({
            deckName: deckNameVar,
            due: today,
            front: frontVar,
            back: backVar
        });
    }
});

New objects will be properly added to the database but for some reason they don't show up.

moment().toDate() returns a Date object which specifies an exact moment in time (eg 1/4/15 at 16:32:12) and not the end or beginning of a day. Keep that in mind when storing your due dates.

Whenever your cards helper runs, it finds an exact moment in time and checks for all cards due less than that exact moment. Adding new cards will not force the helper to rerun because they do not meet the condition. In other words your end is not reactive and too specific.

You can fix this just by making end be the actual end of the day:

var end = moment().endOf('day').toDate();

Furthermore, your helper is using findOne instead of find so you will only ever get one card instead of all cards which match the selector. If you want to see only the last updated card, modify your helper like so:

return Cards.findOne({deckName: this._id, due: {$lte: end}}, {sort: {due: -1}});

Finally, you need to resolve how you are joining decks and cards. In your fixtures you are using an id for deckName , then in your submit you are using the name of the deck, finally in your cards helper you are searching by id. Pick one way and stick with it. I'd recommend calling the field deckId and only using ids (note this requires fixing your submit to find the deck by name and then use the corresponding id).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM