2017-04-19 36 views
0

我有一个非常简单的模板,如下所示;在火焰模板中显示光标数据不适用于Meteor.call()

<template name="editingUsers"> 
    <div class="container"> 
     <div class="jumbotron"> 
      <ul class="custom-list-stye"> 
       {{#each lastEditors}} 
        <li><span><strong>{{docUser.name}}</strong></span> </li> 
       {{/each}} 
      </ul> 
     </div> 
    </div> 
</template> 

我用助手填充HTML中的列表;

Template.editingUsers.helpers({ 
    lastEditors: function() { 
     return Meteor.call('getLastEditors'); 
    } 
}); 

'getLastEditors'方法通过MongoDB查询返回数据;

getLastEditors: function() { 
     if(Meteor.user()) { 
      const lastEditors = Documents.find(
       {docUser: {$exists: true }}, 
       { 
        limit:5, 
        sort: { lastEdit: -1 }, 
       }); 
      return lastEditors; 
     } 
    } 

使用此代码,应该显示为列表的数据不会出现。但是,如果我直接从助手进行Mongo DB查询,一切都会变好。以下是工作帮手代码。 (该包自动发布不删除)

Template.editingUsers.helpers({ 
    lastEditors: function() { 
     return Documents.find(
      {docUser: {$exists: true }}, 
      { 
       limit:5, 
       sort: { lastEdit: -1 }, 
      }); 
    } 
}); 

如你所知,我不能包自动发布继续我要实现Meteor.methods()。我不明白为什么从Meteor.method返回的游标数据没有显示在模板中。我可以提出你的意见吗?

回答

1

替换autopublish程序包的正确方法是实现Publish and Subscribe模式(这是autopublish所要做的),不使用Meteor方法。

您最后的代码示例(您直接在客户端模板助手中搜索Documents集合)非常好。您只需在服务器上至少使用这些文档设置发布(如有必要,您可以发布更多内容),并订阅它(通常是在创建模板时)。

作为一个短期解决方法,您可以使用中间的ReactiveVar在模板助手中显示光标。

feed helper from a callback

此外,在客户端,你必须使用Meteor.call与回调。它不会返回任何东西。

请参阅http://docs.meteor.com/api/methods.html#Meteor-call

+0

Thanks @ghybs for your comment! –

+0

感谢您的反馈!如果答案确实解决了您的问题或提供了重要帮助,那么SO的感谢方式也是可以接受的答案。 – ghybs