2016-08-18 85 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'); –