Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase query second level with react redux firebase

I have a firebase database who contains all purchase of my users

purchase
    userId1
         purchaseId1
         purchaseId2
         purchaseId3
   userId2
         purchaseId1
         purchaseId2
         purchaseId3

A purchase object (ex: purchaseId1) contains a price and a specific code

{
 price:100,
 code:'2XBE'
}

I search a query to find all purchase with the code '3XB1'

I use react-redux-firebase

Thanks

like image 278
Lionel B Avatar asked Aug 22 '26 22:08

Lionel B


1 Answers

you need to change the structure of your database

purchase : {
      purchaseId : {
           price:100,
           code:'2XBE'
           userId : 1
      }     
}

now you can do this

firebase.database().ref().child("purchase")
                   .orderByChild("code").equalsTo("3XB1")
                   .on( snap => console.log(snap.val() )
like image 70
Ali Faris Avatar answered Aug 25 '26 11:08

Ali Faris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!