2017-11-07 5 views
0

同じカテゴリIDを持つFirebase(子)のすべての要素を取得する方法はありますか?Firebase APIとVue.JSは、特定の属性値で複数の要素を取得します。

getbyCateg: function(myCateg) { 
     var items = []; 
     FireBase.database().ref('product') 
      .orderByChild('category') 
      .equalTo(myCateg) 
      .once("value", function(snapshot) { 
       var key; 
       snapshot.forEach(function (childSnapshot) { 
        key = childSnapshot.key; 
        return true; 
       }); 
       if (key) { 
        items.push(FireBase.database().ref('product').child(key).toString()); 
       } else { 
        console.log("There is nothing of this category"); 
       } 
      }); 
     return (items); 

.onceは、カテゴリIDの良い最初の要素にアクセスすることを許可しますが、他の要素はどうなりますか?これらのすべての要素の配列を作成したいと思います。

ありがとうございます!

答えて

0

あなたのデータ構造がわからないと、あなたの問題は本当にわかりません。しかし、おそらくこれはすべてです:

getbyCateg: function(myCateg) { 
    var items = []; 
    FireBase.database().ref('product') 
    .orderByChild('category') 
    .equalTo(myCateg) 
    .once("value", function(snapshot) { 
     snapshot.forEach(function (childSnapshot) { 
     key = childSnapshot.key; 
     if (key) { 
      // also, try this items.push(key.toString()) instead of next line 
      items.push(FireBase.database().ref('product').child(key).toString()); 
     } else { 
      console.log("There is nothing of this category"); 
     } 
     }); 
    }); 
    return (items); 
} 
関連する問題