0

I am providing database structure image where parentId is unknown and child Id is known as shown in image. how do I get all data inside this child Id lie date and contents of orders. I know code is wrong. But fetching just Order huge data and then comparing to this id will be not efficient.

let grabbedData = [];
firebase
      .database()
      .ref(`/orders/`) // or may be like this '/orders/CHILD/-MOzRqdxsBROTleyr1ct/'
      .child("-MOzRqdxsBROTleyr1ct")
      .on("value", (snapshot, key) => {
        console.log("snapshot....", snapshot);
        grabbedData.push(snapshot.val());
        
      });

Database sturcture

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

There is no way to query on the key of a grandchild node in this structure. If you want to query order IDs, you will need to have a flat list of orders, instead of storing them under UIDs.

For example:

orders: {
  "-MP8B....PJ2": {
    uid: "EFTwvh....WZ2",
    date: ....,
    orders: ...
  },
  "-MPEB...bbf": {
    ...
  },
  ...
}

Now you can reach the order by their directly, and can still find the orders for a specific user by searching on the uid property.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807