0

我正在迭代一个时刻范围的日期范围并尝试插入文档。我收到以下错误:

Exception while simulating the effect of invoking '/carpool_events/insert' 
Error
 Error: Sorting not supported on Javascript code
    at Error (<anonymous>)
    at Object.LocalCollection._f._cmp     (http://localhost:3000/packages/minimongo/selector.js?    5b3e1c2b868ef8b73a51dbbe7d08529ed9fb9951:251:13)
    at Object.LocalCollection._f._cmp     (http://localhost:3000/packages/minimongo/selector.js?    5b3e1c2b868ef8b73a51dbbe7d08529ed9fb9951:226:36)
    at LocalCollection._f._cmp (http://localhost:3000/packages/minimongo/selector.js?5b3e1c2b868ef8b73a51dbbe7d08529ed9fb9951:218:33)
    at _func (eval at <anonymous> (http://localhost:3000/packages/minimongo/sort.js?08a501a50f0b2ebf1d24e2b7a7f8232b48af9057:63:8), <anonymous>:1:51)
    at Function.LocalCollection._insertInSortedList     (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:616:9)
    at Function.LocalCollection._insertInResults (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:534:31)
    at LocalCollection.insert (http://localhost:3000/packages/minimongo/minimongo.js?7f5131f0f3d86c8269a6e6db0e2467e28eff6422:362:25)
    at m.(anonymous function) (http://localhost:3000/packages/mongo-livedata/collection.js?3ef9efcb8726ddf54f58384b2d8f226aaec8fd53:415:36)
    at http://localhost:3000/packages/livedata/livedata_connection.js?77dd74d90c37b6e24c9c66fe688e9ca2c2bce679:569:25 

这是我的插入循环。我已经通过写入console.log而不是插入来测试循环并且循环工作正常

    'click button.save-addEventDialogue': function(e, tmpl) {

          var start = Session.get("showAddEventDialogue_dateRangeStart");
          var end = Session.get("showAddEventDialogue_dateRangeEnd");
          var dateRange = moment().range(moment(start),moment(end));
          var dateLoopIncrement = moment().range(moment(start),moment(start).add('days',1));

          console.log(dateRange);
          console.log(dateLoopIncrement);

          // Loop through the date range
          dateRange.by(dateLoopIncrement, function(moment) {
            // Do something with `moment`
            var dateToSave = dateRange.start;  

        // Insert the record
            Carpool_Events.insert({
                       owner: Meteor.user().profile.name,
                       owner_id: Meteor.userId(),
                       original_owner: Meteor.user().profile.name,
                       original_owner_id: Meteor.userId(),
                       declined: 0,
                                  date: dateToSave.toDate()
                      });
            dateToSave.add('days',1);
         });            

         // Clear the Session
         Session.set("showAddEventDialogue_dateRangeStart","");
         Session.set("showAddEventDialogue_dateRangeEnd","");

         // Close the dialogue
         Session.set("showAddEventDialogue", false);
       } 

这样做的正确方法是什么?谢谢。

4

2 回答 2

0

从客户端执行批量插入(循环插入)时,似乎会出现问题。我最终做的是使用 Meteor.methods 在服务器端执行插入。无论在客户端上执行此操作的问题是什么,这似乎都可以解决。

我还意识到我不需要使用矩范围来迭代日期。相反,我只是使用时刻来获得天数的差异并对其进行迭代。

客户端中的JS代码:

'click button.save-addEventDialogue': function (e, tmpl) {
  var start = moment(Session.get("showAddEventDialogue_dateRangeStart"));
  var end = moment(Session.get("showAddEventDialogue_dateRangeEnd"));
  var days = end.diff(start, 'days');
  var count = 0;
  var dateToSave = moment(start);

  // Loop through the date range
  for (count; count <= days; count++) {
    Meteor.call('bulkInsertCarpoolEvent', Meteor.user(), dateToSave.toDate());
    dateToSave.add('days', 1);
  };

  // Clear the Session
  Session.set("showAddEventDialogue_dateRangeStart", "");
  Session.set("showAddEventDialogue_dateRangeEnd", "");

  // Close the dialogue
  Session.set("showAddEventDialogue", false);

  }

在服务器上:

Meteor.startup(function () {
  Meteor.methods({
    bulkInsertCarpoolEvent: function (user, date) {

      return Carpool_Events.insert({
        owner: user.profile.name,
        owner_id: this.userId,
        original_owner: user.profile.name,
        original_owner_id: this.userId,
        declined: 0,
        date: date
      });

    }
  });
});
于 2013-02-03T03:51:53.593 回答
0

错误消息Sorting not supported on Javascript code是将 JavaScript 函数 (!) 插入集合的结果 - 例如,通过执行类似Carpool_Events.insert({x: function () { ... }});JavaScript 函数的操作通常不应进入集合。

在您的代码中的某个地方,您可能没有调用函数(例如,Meteor.userId在客户端上编写而不是在Meteor.userId().这个。

我无法在您的代码中直观地找到问题——如果我错了,为了取得更大的进步,复制会很有帮助。

于 2013-02-05T01:37:16.647 回答