2016-05-25 5 views
0

私は構造を持つコレクション「メニュー」を持っている:流星:内部サーバーエラー[500] Meteor.methodsと()

{ 
"_id" : "vJQr7EuieDHMuX2nx" "section" : "vine" "price" : "200" "category" : "bar" 
"_id" : "vJQr7EuieDHMuX4rf" "section" : "beer" "price" : "200" "category" : "kitchen" 
"_id" : "vJQr7EuieDHMuX5tg" "section" : "milk" "price" : "200" "category" : "kbar" 
"_id" : "vJQr7EuieDHMuX4sc" "section" : "beer" "price" : "200" "category" : "kitchen" 
"_id" : "vJQr7EuieDHMuX2xs" "section" : "vine" "price" : "200" "category" : "bar" 
} 

私はこの私のために を繰り返すことなく、ユーザーにのみ「セクション」を表示したいです使用する Menu.aggregate([{$ group:{_ id: "$ section"}}]) どうすればいいですか?別の方法を使用することがありますか?

サーバー側:サーバーで

Meteor.methods({ 
    'getSections': function() { 
    var pipeline = [{$group:{_id:"$section"}}]; 
    var result = Menu.aggregate(pipeline); 
    var sections = []; 
    for (i = 0; i < result.length; i++) { 
     sections.push(result[i]._id); 
    }; 
    console.log(sections); 
    return sections; 
    } 
}); 

結果:[ 'つる'、 'ビール'、 'ミルク']

クライアント側:

Template.MobBarMenu.helpers({ 
     'list': function(){ 
      this.render('MobBarMenu', {}); 
     }, 
     'listSections': function() { 
      Meteor.call('getSections', function (err, data) { 
       console.log(data); 
       // return data; 
      }); 
     } 
    }); 

template name="MobBarMenu" 
      {{#each listSections}} 
       {{this}} 
      {{/each}} 
template 

私にはありません解決策を見つける場所を知る

+0

あなたのサーバーコンソールは、実際のエラーを指すエラーメッセージを持っている必要があります。また、 'meteor debug'を使ってサーバーサイドのコードをデバッグすることもできます。 –

答えて

0

おそらく高度な出版物が必要です。テンプレートコードで、名前付きコレクションを追加します(クライアントのみ)。次に、ヘルパーを作成してデータを取得し、パブリケーションを購読することができます。

​​

次に、これに書き込むパブリケーションを作成します。

Meteor.publish('sections', function() { 
    const handle = Menu.find({}, { 
    fields: { 
     _id: 0 
     section: 1 
    } 
    }).observe({ 
    added: doc => this.added('sections', doc.section, doc), 
    changed: doc => this.changed('sections', doc.section, doc), 
    removed: doc => this.removed('sections', doc.section, doc) 
    }); 
    this.ready(); 
    this.onStop(() => handle.stop()) 
}) 
0

MeteorはMiniMongoを使用しています。これは、nodejs MongoDBドライバの独自のラッパーです。

デフォルトでは、コレクションにはaggregateというメソッドはありません。それはmonbro:mongodb-mapreduce-aggregationのようないくつかのパッケージで見つけることができます。

または(より良い芋)あなたはrawCollection方法METEORするためにネイティブで生のコレクションへのアクセスを取得することができます:

Menu.rawCollection().aggregate(pipeline, function(err, res) { 
    //do stuff here 
}) 
関連する問題