流星订阅使用外部API发布 [英] Meteor Subscribe & Publish with external api

查看:116
本文介绍了流星订阅使用外部API发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to connect my Meteor Subscribe and Publish to my api, the Publish is calling the API and returning the data no problem but I cant seem to load my data on the template.

下面是我的代码.

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>

您需要一个帮助程序来返回名为boards cursor :

You need a helper to return a cursor called boards:

js:

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

这篇关于流星订阅使用外部API发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆