【问题标题】:Meteor: Error while updating documentMeteor:更新文档时出错
【发布时间】:2015-01-06 12:02:49
【问题描述】:

更新对象日期时出错

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

知道为什么吗?

Meteor 更新了正确的 due 字段,但值变为 "Invalid date"

这是完整的错误(Chrome 开发工具):

Exception while simulating the effect of invoking '/cards/update' Error: Sorting not supported on regular expression {stack: (...), message: "Sorting not supported on regular expression"} Error: Sorting not supported on regular expression
    at Error (native)
    at Object.LocalCollection._f._cmp (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:2411:13)
    at Object.LocalCollection._f._cmp (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:2386:36)
    at Object.LocalCollection._f._cmp (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:2378:33)
    at Object.LocalCollection._f._cmp (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:2386:36)
    at Object.LocalCollection._f._cmp (http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:2378:33)
    at http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:1838:54
    at http://localhost:3000/packages/minimongo.js?e8806aa7782b729b2517ebc0cd10b321667f1427:1474:21
    at Array.some (native)
    at Function._.some._.any (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:284:59)

【问题讨论】:

  • 在事件处理程序中记录“incBy”时会发生什么?会不会是只需要parseInt 的字符串/整数问题?
  • @richsilv 感谢您的回复!它是一个字符串。我做了一个改变:var incBy = parseInt(event.target.value); 但它仍然是同样的问题。似乎它是字符串还是 int 都没关系。
  • 啊,是的,这可能无关紧要。见下文...

标签: mongodb meteor momentjs


【解决方案1】:

您正在尝试将 Moment.js 对象存储在数据库中,而不是 Javascript 日期对象,这是您无法做到的。您需要像这样检索底层的 JS 日期对象:

var newDue = moment(today).add(incBy,'days').toDate();

【讨论】:

  • 这行得通,但我很惊讶。我得到这样的夹具数据: ... var today = moment(); Cards.insert({ deckName: meteorId, due: today, front: 'blaa', back: 'blaa blaa' });我的夹具数据工作正常。日期存储为时刻对象。 (到期:Object_d:2015 年 1 月 6 日星期二 21:01:30 GMT+0200 (EET)_isAMomentObject:true_isUTC:false_locale:Object_pf:Object__proto__:Object)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 2018-12-22
相关资源
最近更新 更多