0

I'm new to react-native and firebase, and I am building an app. I stumble upon a problem using "array" in firebase (i know that firebase doesn't work with array), but I want to know if there is some way to retrieve this data.

This is the data I have in the server, and I want to get all the purchases that have the product "id" (in red)

enter image description here

I tried to use the code below and some variants but no success, returns null

firebase.database().ref('purchases').orderByChild("products/id")
.equalTo("-Lgida0VKuZZ6V7K0LkH").on('value', function (rawPurchases)

I want to know if there is a way to get this information.

Thanks.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Please don't post pictures of text or code. Copy the code directly into the question and format it as code. – Doug Stevenson Jun 19 '19 at 03:48
  • Your current data model makes it easy to find all the products in a purchase. It does not make it easy to find all the purchases that have a specific product. You'll want to store a so-called reverse index to allow that use case, with a top-level node `product_purchases`, under which you have each product, and then under there, all the purchases that contain that product. See my answer here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Jun 19 '19 at 11:46

1 Answers1

0
var scoresRef = firebase.database().ref("purchases");
scoresRef.orderByChild("products").on("value", function(snapshot) {
  snapshot.forEach(function(data) {
    console.log("The " + data.id + " data is " + data.val());
  });
});

try this method and review more here

  • Thanks for the response, but i need something that the server will do the job, not the client. As you showed i will get all purchases and then filter on the client – João Victor Jun 19 '19 at 05:44