简体   繁体   English

Meteor:如何使用Momentjs在Mongodb中添加和查询日期

[英]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. moment().toDate()返回一个Date对象,它指定一个确切的时刻(例如16:32:12的1/4/15)而不是一天的结束或开始。 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. 换句话说,你的end不是反应性的,也不是特定的。

You can fix this just by making end be the actual end of the day: 你可以通过使end成为当天的实际结束来解决这个问题:

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. 此外,您的助手使用的是findOne而不是find因此您只能获得一张卡而不是所有与选择器匹配的卡。 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. 在你的灯具中你使用了一个id为deckName ,然后在你的提交中你使用了牌组的名称,最后在你的cards助手中你正在通过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). 我建议调用field deckId并仅使用id(注意这需要修复你的提交以按名称查找套牌然后使用相应的id)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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