0

Example my db firebase

firebase.database().ref('requests')
  .orderByChild('sender')
  .equalTo("viePatHR0qRaBPGvsb992LEM55F3")
  .on("child_added", (snapshot) => {
    console.log(snapshot.key);})

So I'm trying to find the key node under "requests" in the picture: (example: Mt8ElzJCDfSwDgdjjLuu38KUSw83 and e3iFEuoTtJXAkLgOyxoT0LIeQAg1) I used this code, But I got nothing back. What was wrong? What should I use?

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

1 Answers1

3

The .orderByChild query only works on the child level. (no grandchilds) In your case this would be the level with the firebase push-keys e.g. -KwqBYHP...

So if you do .orderByChild('sender') it will not work, since there is no property called sender on this level.

So you have two options depending on your case. If viePatHR0qRaBPGvsb992LEM55F3 is a unique id you can get rid of the firebase pushkeys by using set instead of push to save your data to the database.

If viePatHR0qRaBPGvsb992LEM55F3 is no unique id, then you need to modify your reference.

The modified reference would look like this firebase.database().ref('requests/viePatHR0qRaBPGvsb992LEM55F3').orderByChild('sender')

If this is not what you expect, the only option you have is to rebuild your data structure. Getting rid of the deep nesting by creating for example a property called:

id: "viePatHR0qRaBPGvsb992LEM55F3"

directly on your object (on the same level where the sender property is).

Orlandster
  • 4,706
  • 2
  • 30
  • 45