0
votes

I've seen many posts and solutions that are precisely what I need but when I implement it it doesn't work. I'm using the Firebase Realtime Database and I'm trying to search all "Name" children for a specific name. In this example I added the strings directly, but I'll be using a variable based on user input in the future. Here is my database structure as I see it in firebase:

{
  "kjienk24dfwefn" : {
    "FriendID" : "fr1959l9591i4925a",
    "Name" : "Test Dummy2",
    "email": "[email protected]",
    "Status" : "InGame"
  },
  "kjienk24kdfiwne" : {
    "FriendID" : "fr2812p8092m3604l",
    "Name" : "Test Dummy",
    "email":"[email protected]"
    "Status" : "Online"
  },
  "kjienk24udfjjo" : {
    "FriendID" : "fr7605h9204f8099g",
    "Name" : "Test Dummy3",
    "email": "[email protected]"
    "Status" : "Offline"
  }
}

And here is the code I'm using to query the "Name" (usersRef points to "firebase.database().ref()"). I've seen lots of examples of this online and I'm not sure what I'm doing incorrectly.

usersRef.orderByChild("Name").equalTo("Test Dummy").on("value", function(snapshot) {
    //This is where I expect to see Test Dummy on the console.  
    console.log(snapshot.val())
});

EDIT: Here is the initialization of usersRef. Haven't had any problems reading and writing to the database. Standard firebaseConfig.:

const fbApp = firebase.initializeApp(firebaseConfig);
const db2 = fbApp.database("https://firebasedatabaseurl.com/")
const usersRef = db2.ref();

Test with parent node: enter image description here

Thanks all in advance!

1
I posted an answer below, but in hindsight am not sure if that's what you're struggling with. 1) Can you replace the data structure you have with the actual JSON from the database (as text, no screenshots please)? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your Firebase Database console. 2) It would also be good if we could see in the question how usersRef is initialized. - Frank van Puffelen
hi Frank, I added the export information. Changed the information but this is the exact structure I got when I exported. The "kjie..." are the userId's. - sychordCoder
Hmmm, that looks fine at first glance. Can you also show how you initialize usersRef? - Frank van Puffelen
I see in all the examples I've looked at you add an argument in ref("argument"). But since I'm referencing my main database node I left it blank. Would I just add the name of the database again? - sychordCoder

1 Answers

0
votes

You want to load data from the users node, but that is currently not mentioned anywhere in your code. So Firebase is querying the root of your database, and the child nodes under there (like users) don't have a Name property, so you get no results.

const usersRef = db2.ref().child("users");

Or:

const usersRef = db2.ref("users");