Here is the structure of my database: https://i.sstatic.net/Tduqg.png
I have successfully retrieved all the documents by querying the userId
:
this.orderListRef = firebase.database().ref(`/orderList`);
this.orderListRef.orderByChild('userId').equalTo(`${this.currentUser.uid}`)
However, I am facing difficulty in retrieving all the documents by querying the sub key restId
in the order
array. The codes I tried returned 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())
})
Any help would be greatly appreciated. I want to receive a result that contains "-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:
Following Frank van Puffelen's suggestion to create restaurant
as a child of the orderList ID, I found it to be a good idea but unfortunately, it didn't work.
Here is the updated screenshot of my database: https://i.sstatic.net/Id3kD.png
I have tried the queries below but all of them still return 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 your assistance!