Filter products based on child in Firebase

I am trying to figure out how to filter products based on their 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 request Firebase so that I retrieve only those products for which it has the value child($productId).child(author) equals 12345 ?

I tried the following, but this clearly does not work. I need a way to filter on subdir:

 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); }); 
0
source share
1 answer

Just use orderByChild() :

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

This will order each node child under products by the author property, and then return only those that have the value 12345 .

See the Firebase documentation for queries .

+3
source

Source: https://habr.com/ru/post/1238740/


All Articles