Gabriel Gabriel - 5 months ago 52
Javascript Question

Check if value exists in firebase DB

Is there a method in firebase, which can check if value exist in DB? Firebase has method .exists(), but according to docs it checks only keys. I have the following structure:

{
"users": {
"-KKUmYgLYREWCnWeHCvO": {
"fName": "Peter",
"ID": "U1EL9SSUQ",
"username": "peter01"
},
"-KKUmYgLYREWCnWeHCvO": {
"fName": "John",
"ID": "U1EL5623",
"username": "john.doe"
}
}
}


I want to check if ID with value
U1EL5623
exists.

Answer

There is no explicit exists() method but you can work with the following logic to handle it.

ref.child("users").orderByChild("ID").equalTo("U1EL5623").once("value", function(snapshot) {
    var userData = snapshot.val();
    if (userData){
      console.log("exists!");
    }
});
Comments