Meteor Subscribe & Publish with external api

老子叫甜甜 提交于 2020-01-05 04:32:04

问题


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.

Below is my code.

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:


your html template:

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

You need a helper to return a cursor called boards:

js:

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


来源:https://stackoverflow.com/questions/39027694/meteor-subscribe-publish-with-external-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!