2016-06-20 4 views
0

流星内のjsonファイルからオブジェクト 'Posts'内のオブジェクト '1 Post'をフェッチしようとしています。この。 メテオ:アンダースコア_findループオブジェクトを通した反復は、クロームコンソールでしか動作しません。アプリケーションでは未定義です

enter image description here

は、私はそれを取得するfindWhereを強調使用して、それを行うための効果的な方法を発見しました。私は流星にこれを入れたときに、これはしかし、私はこれは私が

Template.CategoryArticleSingle.helpers({ 
      articles: function() { 
      var id = FlowRouter.getParam('ID') 
      var category = FlowRouter.getParam('category') 
      console.log(CategoryCollection.find().fetch()); 
       let match = _.findWhere(_.findWhere(CategoryCollection.find().fetch(), {"_id":category}).posts,{"ID": id}); 
      console.log("match",id,category,match); 
      return match; 
      } 
     }); 

なぜ私は未定義

取得していますを使用したコードで未定義

を得ているコード

_.findWhere(_.findWhere(CategoryCollection.find().fetch(), 
{"_id":"CategoryPublication-5"}).posts,{"ID":46}); 

です更新。

これは正しいですか?私はすべてのリンクのためにそれを使用することができますので、私は47 idを、ちょうどidで置き換えました。

「カテゴリ」を取得しているImは読み取り専用エラーです。

Template.CategoryArticleSingle.helpers({ 
      articles: function() { 
      var id = FlowRouter.getParam('ID') 
      var category = FlowRouter.getParam('category') 
      console.log(CategoryCollection.find().fetch()); 
      const category = CategoryCollection.find().fetch().find(c => c._id === id); 
      let post = null; 

      if (category) { 
       post = category.posts.find(p => p.ID === id); 
      } 
      console.log("post",id,category,post); 
      return post; 
      } 
     }); 

答えて

0

lodash /アンダースコアのfindWhereを使用する必要はありません。この機能はES2015に組み込まれています。また、見やすくするためにコードをいくつかの行に分割することも考えられます。

const category = CategoryCollection.find().fetch().find(c => c._id === 'CategoryPublication-5'); 
let post = null; 

if (category) { 
    post = category.posts.find(p => p.ID === 47); 
} 
+0

こんにちは@ffxsamご返信ありがとうございます。投稿の更新されたコードを見て、アドバイスしてください。私はそれを正しくしたのか分からない。より多くの文脈を与えるために –

+0

。この配列には3つのレベルがあります。私の前の質問を見て、あなたにコンテキストを教えてください。あなたの助けは高く評価されています。http://stackoverflow.com/questions/37902133/meteor-there-is-no-route-for -the-path-error-to-access-single-article-withi/37902963?noredirect = 1#comment63271715_37902963 –

関連する問題