0

This is the structure of the database,

Content {
   "randomID" {
      "randomID2" {
         "text": "Hello World"
             }
      }
}

I'm trying to do a query in cloud functions, to check if text is equalTo a specific value but it always returns null

admin.database().ref('Content').orderByChild('text').equalTo("someText").once('value').then(snapshot => {

     console.log(snapshot.val()); -> this returns null

})

I'm pretty sure the issue is that I have two random ID's there, also wildcards don't work for .once('value') like they do for onUpdate I believe. Or I'm doing something wrong as I'm quite new to Javascript.

Don
  • 13
  • 5
  • why do u have two random ids? – Peter Haddad Feb 03 '18 at 16:08
  • @peterhaddad randomID is the userID, inside userID, I have all the random2 ids which point to a specific conversation. – Don Feb 03 '18 at 16:13
  • Firebase Database query check the **direct child nodes** of the location you run them against. So in your case it checks whether `/Content/randomID` has a property `text` with value `sometext`. In the data you shared it doesn't have that, so nothing is returned. Firebase queries do not test against nested child nodes. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Feb 03 '18 at 16:59
  • @FrankvanPuffelen so I should fetch without OrderByChild first, then do another fetch .ref('Content/' + snapshot.val()).once('value'), won't this cause a lot of data usage? Because in my app, "Content" has a lot of nodes. – Don Feb 03 '18 at 17:20
  • You'll need to restructure (or add an additional structure) to allow the use-case you want. Downloading too much data is a bad idea, so add a structure that allows the query. The answer I linked has an example of that. – Frank van Puffelen Feb 03 '18 at 17:24
  • Okay, thanks a lot! – Don Feb 03 '18 at 17:29

1 Answers1

1
admin.database().ref('Content').orderByChild('text')

should be changed to

admin.database().ref('Content/{randomID}/{randomID2}').orderByChild('text')

you can't skip the ids in the reference

Alex Mounir
  • 1,243
  • 1
  • 14
  • 20