Meteor : how to publish custom JSON data?

寵の児 提交于 2019-12-01 00:12:18

Meteor Pubs/Subs are made for data reactivity. If you don't need reactivity but some one-shot data the server computes for you and sends back, you need a method!

// Server code
Meteor.methods('getComplexData', function() {
  var complexData = { /* make your complex data */ };
  return complexData;
});
// Client code
Meteor.call('getComplexData', function(err, data) {
  if(err) {
    // Handle error
  }
  else {
    Session.set('complexData', data);
  }
});

More about Session

Using a method probably makes more sense if the data is not going to be changed very often. The publish/subscribe pattern is also possible here but instead of returning a cursor or anything, you will need to use the publication "gears" manually, like this:

Meteor.publish("myCustomPublication", function () {
  // here comes some custom logic
  this.added("myCollection", someUniqueId, someCustomData);
  this.ready(); // without this waitOn will not work
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!