Meteor 使用外部订阅和发布 api

Meteor Subscribe & Publish with external api

我正在尝试将我的 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);
    }
  });
}

您的 html 模板:

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

您需要一个助手来 return 一个名为 boards:

cursor

js:

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