2016-02-21 93 views
0

我有用户表访问对象模板

Template.followers.helpers({ 
    followers: function() { 
     return Meteor.users.find({_id: Meteor.userId()},{_id:0,followers:1, profile:1}); 
    } 
}); 

现在我要显示的数据,其结构如下单个文件

{ 
    "profile" : { 
     "name" : "new user", 
     "gender" : "" 
    }, 
    "followers" : [ 
     { 
      "id" : "yQLrjsbAnKHW7Zoef", 
      "name" : "vid vid" 
     }, 
     { 
      "id" : "bGLrjsbAnKHW7Zoef", 
      "name" : "sid sid" 
     } 
    ] 
} 

和我的助手功能只是追随者为:

name: Vid 
name: Sid 

基本上我想访问我的模板中的追随者数组中的元素。 目前它的

{{#each followers}} 
     {{ profile.name}} 
     {{ followers}} 
{{/each}} 

回答

0

修正了它。

问题是Meteor.users.find()只返回有限的字段。首先,我尝试在发布users的方法中使用字段说明符,但这不起作用。所以,以下是我做过什么:

在服务器端,声明新的变量为: 输入代码在这里

Meteor.publish('UserProfiles', function() { 
    return UserProfiles.find({},{ 
    fields : { 
     'followers' : 1, 
     'profile' : 1, 
     'createdAt' : 1 
    } 
}); 

:在这里我指定我需要的领域

UserProfiles : = Meteor.users; 

增加了新的发布方法});

添加以下行客户端:

UserProfiles : = Meteor.users; 
Meteor.subscribe("UserProfiles"); 

然后在我的文件,我开了查询并返回:

users: function(){ 
    return UserProfiles.find(selector, { 
     fields : { 
      'followers' : 1, 
      'profile' : 1, 
      'createdAt' : 1 
     } 
    }); 
} 

内模板代码:

{{#each users}} 
    Follower of {{profile.name}} 
    {{#each followers}} 
      {{> follower}} 
    {{/each}} 
{{/each}} 
0

这是你的模板代码

{{#each followers}} {{ profile.name}} {{ followers}} {{/each}} 

正确的模板代码

{{#each followers}} 
    {{profile.name}} 
    {{#each this.followers}} 
    {{name}} 
    {{/each}} 
{{/each}} 

这会给你的个人资料持有人的名称和追随者该配置文件。

+0

它不工作。它只是给我第一个追随者的细节,这是'VID VID'。 – Viddesh

+0

它为我工作,给我所有的人和他们的追随者名单。 –

+0

你有帮助吗? – Viddesh