Filter products based on the sub child in Firebase

2019-08-24 04:02发布

I am trying to figure out how to filter products based on their sub child nodes in Firebase.

My setup is as follows:

products/
     product1
        /author: 12345
        /title: "Awesome"
        /description: "more awesome"
     product2
        /author: 67890
        /title: "Awesome"
        /description: "more awesome"

How can I query Firebase such that I retrieve only the products for which it holds that child($productId).child(author) equals 12345?

I tried the following, but that obviously does not work. I need a way to filter on the subchild:

var ref = new Firebase(FBURL);

// Attach an asynchronous callback to read the data at our posts reference
ref.child("products").child('$productId').child('author').equalTo(12345).on("value", function(snapshot) {
      console.log(snapshot.val());
    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
});

1条回答
forever°为你锁心
2楼-- · 2019-08-24 04:08

Simply use orderByChild():

ref.child("products").orderByChild('author').equalTo(12345)...

This will order every child node under products by its author property and then return only the ones that have a value of 12345.

See Firebase's documentation on queries.

查看更多
登录 后发表回答