如何在循环中插入文档?

时间:2013-02-02 13:39:07

标签: meteor

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

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

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

2 个答案:

答案 0 :(得分: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
      });

    }
  });
});

答案 1 :(得分:0)

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

在您的代码中的某处,可能存在一个错误,您没有调用函数(例如,在客户端上编写Meteor.userId而不是Meteor.userId()。)我的猜测是在过程中让您的代码在服务器上运行,您巧合地修复或避免这种情况。

我无法直观地找到代码中的问题 - 如果我错了,为了取得更多进展,进行复制会很有帮助。