Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the 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 U1EL5623exists.

like image 212
Runtime Terror Avatar asked Jun 19 '16 17:06

Runtime Terror


People also ask

How can I check if a value exists already in a firebase data class Android?

child(busNum). exists() tests for the existence of a value at location BusNumber/<busNum> . It will not be true unless busNum is one of the keys created by push() .

What is DataSnapshot in firebase?

firebase. database. DataSnapshot. A DataSnapshot contains data from a Database location. Any time you read data from the Database, you receive the data as a DataSnapshot .

How do I find my firebase database?

To see your current Realtime Database connections and data usage, check the Usage tab in the Firebase console. You can check usage over the current billing period, the last 30 days, or the last 24 hours.

What is uf8ff firebase?

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText .


2 Answers

The exists() method is part of the snapshot object which is returned by firebase queries. So keep in mind that you won't be able to avoid retrieving the data to verify if it exists or not.

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

Observations:

In case you are in a different scenario which you have the exact ref path where the object might be, you wont need to add orderByChild and equalTo. In this case, you can fetch the path to the object directly so it wont need any search processing from firebase. Also, if you know one of the properties the object must have you can do as the snippet below and make it retrieve just this property and not the entire object. The result will be a much faster check.

//every user must have an email firebase.database().ref(`users/${userId}/email`).once("value", snapshot => {    if (snapshot.exists()){       console.log("exists!");       const email = snapshot.val();    } }); 
like image 69
adolfosrs Avatar answered Sep 22 '22 01:09

adolfosrs


This is a similar solution if you want to check if an email exists in firebase

firebase.app().database().ref("shops").orderByChild("email")    .equalTo(user.email).once("value", snapshot => {              const userData = snapshot.val();              // Check if it is a SHOP.             if (userData) {               console.log("Shop logged in!");               this.setState({                 isAdminLoggedIn: false,                 isUserLoggedIn: false,                 isShopLoggedIn: true,                 isNoneLoggedIn: false               });              // Check if it is a USER.             } else {               console.log("User logged in");               this.setState({                 isAdminLoggedIn: false,                 isUserLoggedIn: true,                 isShopLoggedIn: false,                 isNoneLoggedIn: false               });             }         }); 
like image 35
Riccardo Persiani Avatar answered Sep 21 '22 01:09

Riccardo Persiani