2016-07-23 30 views
0

我有一个对象集合,名为类别在列表中。在我的html中,每个类别对象都是一个列表项目,这个类别对象中有其他对象,这些是属于该类别的类别ID,名称和帖子数组。见图片。在Meteor.user字段中保存了一个对象的ID,在模板中调用了该ID,如何显示对象的数据而不是ID?

enter image description here

在每个类别列表项目是一个按钮,用户可以点击,这然后保存类别ID在Meteor.user场叫名字。

<template name="CategoriesMain"> 
<ul> 
    {{#each articles}} 
    <li> 
     <a href="/catsingle/CategorySingle/{{_id}}"><h2>{{name}}</h2></a> 
     <button type="button" class="toggle-category">Add to User Field</button> 
    </li> 
    {{/each}} 
</ul> 
</template> 

在帮助我

Template.CategoriesMain.events({ 
    'click .toggle-category': function(e){ 
     //take the category id on click 
      var ob = this._id; 
      //make it into array 
      var id = $.makeArray(ob); 
      e.preventDefault(); 
      Meteor.call('addingCategory', id, function(error, user){ console.log(id)}); 
     }, 
}); 

然后在我的方法来实现这个我有这个。

Meteor.methods({ 
    addingCategory: function(name) { 
     Meteor.users.update({ 
     _id: Meteor.userId() 
    }, 
    { 
     $addToSet: { 

     name: name 
     } 
    }); 
    } 
}); 

as you can see, the category ids are being inputed into the user data as an array

每个用户具有其中保存的类别ID出现的时间线。

<template name="userTimeline"> 
    {{#if currentUser}} 
    <div class="timeline-user"> 
<p>{{name}}</p> 
    </div> 
{{/if}} 
</template> 

enter image description here

在助手

Template.userTimeline.helpers({ 

    name: function() { 
    return Meteor.user().name; 

//this is what im experiment with to link the category id's in the timeline with the category ids from the Category collection but i'm not sure if im going the right direction. 
    var name = FlowRouter.getParam('_id') 
    return CategoryCollection.find({_id: id}).fetch(); 
    } 
}); 

我的问题是

,而不是显示类别ID,可以以某种方式得到这些类别ID,在该类别所属的对象和IE的帖子?我知道我必须以某种方式与该类别的链接通过用户收集的ID

EDIT

我已经修改了我的流星方法申报“类别”作为这样一个数组:

Accounts.onCreateUser(function(options, user){ 
    user.category = []; 
    return user; 
}); 


Meteor.methods({ 
    addingCategory: function(category) { 
     console.log(Meteor.userId()); 
     Meteor.users.update({ 
     _id: Meteor.userId() 
    }, 
    { 
     $addToSet: { 
     category: category 
     } 
    }); 
    } 
}); 

这是Meteor.users的样子,看看“类别”字段。

enter image description here

我已经修改了我的助手,这是引发错误如下:

Template.userTimeline.helpers({ 

    category(){ 
    return CategoryCollection.find({ _id: { $in: this.category }}); // a cursor of categories 
    } 
}); 

其中CategoryCollection收集持有的类别。

因为我在我的方法中声明“category”为数组,所以我不再像以前那样将每个类别id更改为数组,因此我将模板事件更改为此。

Template.CategoriesMain.events({ 
    'click .toggle-category': function(e){ 
      var ob = this._id; 
      console.log(ob); 
      e.preventDefault(); 
      Meteor.call('addingCategory', ob, function(error, user){ console.log(ob)}); 
     } 
}); 

我的发布已更改为此:我不知道是否应在此处使用$?

Meteor.publish(null, function() { 
    return Meteor.users.find({_id: this.userId}, {fields: {category: 1}}); 
}); 

我的HTML已被更改为这个:

<template name="userTimeline"> 
    {{#if currentUser}} 
    <div class="timeline-user"> 
    {{#each category}} 
    Count: {{count}} 
    Description: {{description}} 
    Link: {{link}} 
    {{#each posts}} 
     ID: {{id}} 
     Title: {{title}} 
    {{/each}} 
    {{/each}} 
    </div> 
{{/if}} 
</template> 

回答

1

可以显示通过遍历类别ID阵列上,并使用一个辅助返回整个类别对象与用户的类别。然后在里面,你可以循环的帖子。

<template name="userTimeline"> 
    {{#if currentUser}} 
    <div class="timeline-user"> 
    {{#each categories}} 
    Count: {{count}} 
    Description: {{description}} 
    Link: {{link}} 
    {{#each posts}} 
     ID: {{id}} 
     Title" {{title}} 
    {{/each}} 
    {{/each} 
    </div> 
{{/if}} 
</template> 

那么你的帮手:

template.userTimeline.helpers({ 
    categories(){ 
    if (this.category && 
     typeof(this.category) === "object" && 
     this.category.length){ // check to see if the list of categories is an array 
     return categories.find({ _id: { $in: this.category }}); // a cursor of categories 
    } else { 
     console.log(this.category); 
     return null; 
    } 
    } 
}); 

记住,对于userTimeline模板的数据上下文thisMeteor.user()对象,以便this.name将是类别ID数组中Meteor.user().name

+0

嗨MichelFloyd 。非常感激。我收到这个错误。 **模板助手中的异常:错误:$ in需要一个数组**它可以有什么可以做什么,你在另一篇文章中说过吗? **“你只是返回一个字符串数组而不是对象”**名称? –

+0

你可以告诉我们'Meteor.user()。name'确实是一个数组吗?你可以有一些只有一个字符串或名称未定义的记录? –

+0

没有问题,请看看编辑部分和下面的原始问题@MichelFloyd –

相关问题