0

This is how my DB look like: This is how my DB look like:

I manage to get all the document by query the userId:

this.orderListRef = firebase.database().ref(`/orderList`);
this.orderListRef.orderByChild('userId').equalTo(`${this.currentUser.uid}`)

But i having problem to get all the document by query the sub key restId in the order array, i did try below codes but it just return null:

this.orderListRef.orderByChild('order/restId').equalTo('-L6vQHDBv9WugS_yTibn').on('value', snap => {
      console.log(snap.val())
    })

and

this.orderListRef.child('order').orderByChild('restId').equalTo('-L6vQHDBv9WugS_yTibn').on('value', snap => {
  console.log(snap.val())
})

Please help.. I would like to get return that contain "-L6vQHDBv9WugS_yTibn" in the restId key only, like:

-L9IZKHkueFppGMJY3Z9: {deliveryAddress: "Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia", dtcreate: 1522894198149, order: Array(2), userId: "AeMgnwHy3Hav3FE9DyTMJVdp4QX2", userOrderNumber: "pGMJY3Z9"}
-L9I_DcXgx_fjsmsn2rm: {deliveryAddress: "Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia", dtcreate: 1522894433661, order: Array(2), userId: "AeMgnwHy3Hav3FE9DyTMJVdp4QX2", userOrderNumber: "jsmsn2rm"}
-L9IksHXG4WZo0ejfmk1: {deliveryAddress: "Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia", dtcreate: 1522897487221, order: Array(2), userId: "AeMgnwHy3Hav3FE9DyTMJVdp4QX2", userOrderNumber: "o0ejfmk1"}

Thank you in advance!

Edited 1: After Frank van Puffelen suggest the method of creating restaurant as a child of the orderList > ID, i found that it is good idea and i tried, but still no luck.

Heres the new screenshot of my DB: enter image description here

I have tried with below queries but everyone still returning null:

this.orderListRef.orderByChild('restaurant').equalTo('rest001').once('value', snap => {
      console.log(snap.val())
    })

this.orderListRef.child('restaurant/rest001').once('value', snap => {
      console.log(snap.val())
    })
this.orderListRef.child('restaurant').equalTo('rest001').once('value', snap => {
      console.log(snap.val())
    })

this.orderListRef.child('restaurant').orderByChild('rest001').once('value', snap => {
      console.log(snap.val())
    })

Thank you for responding!

Jerry
  • 1,455
  • 1
  • 18
  • 39

1 Answers1

0

There is no field order/restId. There are fields order/0/restId and order/1/restId, but since that isn't a fixed path you can't query on them.

Your current data structure is suited to finding the restaurant IDs for a given order. It is not well suited to finding the orders for a given restaurant ID. To allow that, you should expand your data structure to have the inverted data:

restaurants
  restaurantId1
    order1: true
    order3: true
  restaurantId2
    order2: true

Now you can find the orders for a given restaurant by simply looking them up under /restaurants/$restaurantId.

For more on this see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • unfortunately i still don get the result.. i did try following query: `this.orderListRef.orderByChild('restaurant').equalTo('rest001').once('value', snap => { console.log(snap.val()) })` , ` this.orderListRef.child('restaurant/rest001').once('value', snap => { console.log(snap.val()) })` , ` this.orderListRef.child('restaurant').equalTo('rest001').once('value', snap => { console.log(snap.val()) })` , `this.orderListRef.child('restaurant').orderByChild('rest001').once('value', snap => { console.log(snap.val()) })` – Jerry Apr 05 '18 at 13:58
  • fyi, i added `restaurant` as a child of orderList. `orderList > Id > restaurant` . then i added `rest001` and `rest002` as the child of `restaurant`, and `{order1:true}` as child of `rest001` and `rest002` – Jerry Apr 05 '18 at 14:01
  • Please update your question with the new information. As you might notice, longer code in comments is almost impossible to read. – Frank van Puffelen Apr 05 '18 at 14:02
  • I have updated the question, please have a check. Thanks for responding! – Jerry Apr 05 '18 at 14:46
  • None of those queries will indeed work, since you're constantly calling `child()` or `orderByChild()` on a path that doesn't exist in the context where you call it. I recommend adding an inverted data structure, which is explained in my answers to both questions I linked. – Frank van Puffelen Apr 05 '18 at 15:58