Meteor订阅&使用外部api发布

时间:2016-08-18 21:20:36

标签: meteor meteor-blaze

我正在尝试将我的Meteor订阅和发布连接到我的api,发布正在调用API并返回数据没有问题,但我似乎无法在模板上加载我的数据。

以下是我的代码。

boards.js

import './boards.html';

Tracker.autorun(function() {
  Meteor.subscribe('getUserBoards');
});

boards.html

<template name="userBoards">
  {{#each boards}}
    {{this.id}}
  {{/each}}
</template>

index.js

if (Meteor.isServer) {
  Meteor.publish('getUserBoards', function getBoards() {
    var self = this;
    try {
      var response = HTTP.get(Meteor.settings.private.api.url+'users/'+this.userId+'/boards/');

      _.each(response.data.boards, function(item) {
        var doc = {
          id: item._id,
          name: item.name,
          urlFriendlyName: item.urlFriendlyName,
          access: item.access,
          backgroundImage: item.backgroundImage,
          products: item.products,
          sharedCount: item.meta.shared,
          totalProducts: item.meta.totalProducts,
          dateAdded: item.meta.dateAdded
        };

        self.added('boards', item._id, doc);
      });

      self.ready();

    } catch(error) {
      console.log(error);
    }
  });
}

1 个答案:

答案 0 :(得分:2)

你的html模板:

<template name="userBoards">
  {{#each boards}}
    {{this.id}}
  {{/each}}
</template>

您需要帮助程序才能返回名为boards游标

JS:

Template.userBoards.helpers({
  boards(){
    return Boards.find();
  }
});