I\'m using the Realtime Database with Google\'s Firebase, and I\'m trying to check if a child exists.
My database is structured as the following
- /
I have some suggestions by using firebase.You check it from firebase.
We can test for the existence of certain keys within a DataSnapshot using its exists() method:
A DataSnapshot contains data from a Firebase database location. Any time you read data from a Firebase database, you receive the data as a DataSnapshot.
A DataSnapshot is passed to the event callbacks you attach with
on()oronce(). You can extract the contents of the snapshot as a JavaScript object by calling itsval()method. Alternatively, you can traverse into the snapshot by callingchild()to return child snapshots (which you could then callval()on).A DataSnapshot is an efficiently-generated, immutable copy of the data at a database location. They cannot be modified and will never change. To modify data, you always use a Firebase reference directly.
exists() - Returns true if this DataSnapshot contains any data. It is slightly more efficient than using snapshot.val() !== null.
Example from firebase documentation(javascript example)
var ref = new Firebase("https://docs-examples.firebaseio.com/samplechat/users/fred");
ref.once("value", function(snapshot) {
var a = snapshot.exists();
// a === true
var b = snapshot.child("rooms").exists();
// b === true
var c = snapshot.child("rooms/room1").exists();
// c === true
var d = snapshot.child("rooms/room0").exists();
// d === false (because there is no "rooms/room0" child in the data snapshot)
});
Also please refer this page(already mentioned in my comment)
Here there is an example using java.
Firebase userRef= new Firebase(USERS_LOCATION);
userRef.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.getValue() !== null) {
//user exists, do something
} else {
//user does not exist, do something else
}
}
@Override
public void onCancelled(FirebaseError arg0) {
}
});
I hope you got an idea now.