2016-05-07 9 views
-1

誰でも助けてください。この前に私は質問をしていますが、私はこの解決策を見つけることができません。コレクションの変数を数えるコードを作成しました。グループではなく1つずつカウントすると結果が得られます。それは私のコードは、私はこれをカウントしたいと思うが、コードは私にどのような再開を与えられていません。私はしたい。このような結果:流星:コレクションからデータを集める

PTR 1 KOM 4

This my code: 
<template name="laporankategori"> 
    <table class="table"> 
    <thead> 
     <tr> 
     <th>Jenis Peralatan</th> 
     <th>Kuantiti</th> 
     </tr> 
    </thead> 
    <tbody> 
     {{#each profil}} 
     <tr> 
      <td>{{PTR}}</td> 
      <td>{{KOM}}</td> 
     </tr> 
     {{/each}} 
    </tbody> 
    </table> 
</template> 

// JS

Template.laporankategori.helpers({ 
    profil: function() { 
    return Profil.find({kategori: { $in: ['PTR', 'KOM'] } }).count(); 
    } 
}); 
+0

どこ 'PTR'と' KOM'ヘルパー/コンテキスト変数がありますか? –

+0

を意味しますか?私はすでに 'profil:function(){ return Profil.find({カテゴリ: 'PTR'})のようにこのコードを使用しているからです。 } 'と動作しています。今度はこのコード 'profil:function(){ return Profil.find({カテゴリ:{$ PTR '、' KOM '}}})を試してみましょう。 } 'と動作しません。私はどこに問題があるのか​​分かりません。 – nurul

答えて

0
<template name="laporankategori"> 
<table class="table"> 
    <thead> 
    <tr> 
    <th>Jenis Peralatan</th> 
    <th>Kuantiti</th> 
    </tr> 
    </thead> 
    <tbody> 
    {{#each profil}} 
    <tr> 
     <td>{{count}}</td> 
    </tr> 
    {{/each}} 
    </tbody> 
</table> 
</template> 

// JS

Template.laporankategori.helpers({ 
profil: function() { 
    var PTR = { 
    count: Profil.find({kategori: { $in: ['PTR'] } }).count() 
    }; 
    var KOM = { 
    count : Profil.find({kategori: { $in: ['KOM'] } }).count() 
    }; 
    var resultArr = [PTR, KOM]; 
    return resultArr; 
} 
}); 
0

あなたはどちらかを返す必要があり{{#each ...}}あなたのヘルパーを反復処理しているときはいつでもカーソルまたは配列。あなたの助けはスカラーを返しています:カウント。あなたの{{#each }}ブロックには、{{PTR}}{{KOM}}がありますが、それらは存在しません。

私はあなたが実際にこの場合のカウントとあなたのヘルパーを探していなかったことだけであることを疑う:

Template.laporankategori.helpers({ 
    profil: function() { 
    return Profil.find({kategori: { $in: ['PTR', 'KOM'] } }); 
    } 
}); 

また、あなたは、多くの場合、テンプレートで以来、ヘルパーで物事をカウントする必要はありません。 {{profil.count}}を参照して、カーソルの数を直接取得することができます。

+0

'group aggregate count'の使い方を教えてください – nurul

+0

別の質問です。 –

関連する問題