2016-07-23 6 views
0

カテゴリと呼ばれるオブジェクトのコレクションがあります。私のHTMLでは、各カテゴリオブジェクトはリストアイテムであり、このカテゴリオブジェクトには他のオブジェクトがあります。これらはカテゴリID、名前、およびそのカテゴリに属する​​投稿の配列です。画像を参照してください。各カテゴリーのリスト項目でオブジェクトのIDをMeteor.userフィールドに保存し、テンプレート内のIDを呼び出しましたが、IDの代わりにオブジェクトのデータを表示するにはどうすればよいですか?

enter image description here

ユーザーがクリックできるボタンで、これは、名前と呼ばれるMeteor.userフィールドにカテゴリIDが保存されます。これを達成するための

<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のポストを得ることができます?

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 
     } 
    }); 
    } 
}); 

この:私はカテゴリ

EDIT

のそれと利用者によって収集idが、私はそうのような配列として、「カテゴリ」を宣言するために私の流星法を改正している何とかリンクする必要があります知っていますMeteor.usersがどのように見えるかは、「カテゴリ」フィールドを見てください。

enter image description here

私はエラーを投げている私のヘルパー、で次のように改正されています:CategoryCollectionカテゴリを保持コレクションある

Template.userTimeline.helpers({ 

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

します。

私は自分のメソッドで配列として "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)}); 
     } 
}); 

私のパブリッシュはこれに変更されました:私は$ inを使うべきかどうか分かりませんか?

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テンプレートのデータコンテキストthisはそうthis.nameMeteor.user().name

+0

こんにちはMichelFloyd内のカテゴリIDの配列になるだろうMeteor.user()対象であることを忘れないでください。とても有難い。私はこのエラーが発生しています。 **テンプレートヘルパーでの例外:エラー:$配列に配列が必要**他の投稿で前に述べたことを何かすることができますか? ** "名前の代わりにオブジェクトの代わりに文字列を返すだけです" ** –

+0

'Meteor.user()。name'に実際に何が入っているのかを見せて、それが配列であることを確認できますか?文字列だけを持つレコードや名前が定義されていないレコードがありますか? –

+0

No probですので、元の質問の編集セクションをご覧ください。@MichelFloyd –

関連する問題