Meteor.publish()在延迟回调中抛出异常

时间:2015-06-16 06:37:06

标签: meteor

我在服务器控制台中发现了这个错误:

  

延迟回调中的异常:TypeError:undefined不是函数       at packages / ddp / livedata_server.js:1054:1       at Array.forEach(native)       在函数。 .each。 .forEach(packages / underscore / underscore.js:105:1)       在[object Object] ._。extend._callStopCallbacks(packages / ddp / livedata_server.js:1053:1)       在[object Object] ._。extend._deactivate(packages / ddp / livedata_server.js:1043:1)       at packages / ddp / livedata_server.js:803:1       在函数。 .each。 .forEach(packages / underscore / underscore.js:113:1)       在[object Object] ._。extend._deactivateAllSubscriptions(packages / ddp / livedata_server.js:802:1)       在packages / ddp / livedata_server.js:444:1       在[object Object] ._。extend.withValue(packages / meteor / dynamics_nodejs.js:56:1)

这里是我使用的代码:

/* Source: http://phucnguyen.info/blog/how-to-publish-to-a-client-only-collection-in-meteor/ */
var subs = {};
Meteor.publish(

// THROW AN 'EXCEPTION IN DERFER CALLBACK: ...'
  'helperPublication',  
  function () {
    var subscription = this;
    subs[subscription._session.id] = subscription;
    Datasets.find().map(function (dataset) {
      subscription.added(
        'subdatasets',
        dataset._id,
        {
          name: dataset.name,
          data: [],   // To avoid "Uncaught TypeError: Cannot read property 'length' of undefined" error on load
          peaks: []   // Idem
        }
      )
    });
    subscription.onStop();
    subscription.ready()
});

您可以在以下meteorpad中找到整个应用:http://meteorpad.com/pad/6NDneym2qEW7pF9JM/ClearChrom

1 个答案:

答案 0 :(得分:1)

好的,所以有了这个信息;我认为最好的方法是为data分别收集。{1}}。因为这样您就可以轻松更改应显示的数据量。该集合的出版物可能如下所示:

Meteor.publish('data', function publishData (limit) {
  return DataCollection.find({}, {
    fields: {
      name: 1,
      data: 1,
      peaks: 1
    },
    limit: limit
  })
});

请注意,发布的回调采用参数limit。您现在可以这样订阅它:

Meteor.subscribe('data', 3)

无论何时您需要更多数据,您都可以:

Meteor.subscribe('data', 6)

所以这个解决方案是反应性的,非常干净(至少在我看来)。

我还检查了您现有的脚本:

var subs = {};

Meteor.publish(
  'helperPublication',
  function (limit) {
    var subscription = this;
    subs[subscription._session.id] = subscription;
    // I'd use forEach here, because you're not modifying the document
    Datasets.find().forEach(function (doc) {
      subscription.added(
        'subdatasets',
        doc._id,
        {
          name: doc.name,
          // Whith the slice function you can get a subset of the data array
          data: doc.data.slice(0, limit),
          peaks: doc.peaks
        }
      )
    });
    // Calling `subscription.onStop` without an argument doesn't do anything
    // Except maybe through an error
    subscription.onStop(function () {
      // clean the subscription from subs again
      delete subs[subscription._session.id];
    });
    // This shouldn't be necessary.
    // subscription.ready()
});

这仍有一些问题。首先,我建议你尝试使用前面的下划线来避免流星属性。 可能会在以后的版本中删除或更改