2016-08-18 76 views
1

我想連接我的流星訂閱和發佈到我的API,發佈調用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); 
    } 
    }); 
} 
+0

哪裏是你的返回'boards'到'userBoards'模板模板幫手? –

+0

@MichelFloyd這就是我一定會想念的,我該如何去做這件事?對不起,我是流星新手。 –

回答

2

你的HTML模板:

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

你需要一個幫手,返回光標稱爲boards

JS:

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

工作,感謝很多驅使我堅果。我還需要添加Boards = new Mongo.Collection('boards'); –