Search code examples
meteormeteor-blaze

Meteor Subscribe & Publish with external 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.

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);
    }
  });
}

Solution

  • 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();
      }
    });