Skip to content

Instantly share code, notes, and snippets.

@NguyenTungs
Forked from anantn/firebase_detect_data.js
Created March 15, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NguyenTungs/2d1658a1f0cb67de943bc1d52b014410 to your computer and use it in GitHub Desktop.
Save NguyenTungs/2d1658a1f0cb67de943bc1d52b014410 to your computer and use it in GitHub Desktop.
Firebase: Detecting if data exists. This snippet detects if a user ID is already taken
function go() {
var userId = prompt('Username?', 'Guest');
checkIfUserExists(userId);
}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users';
function userExistsCallback(userId, exists) {
if (exists) {
alert('user ' + userId + ' exists!');
} else {
alert('user ' + userId + ' does not exist!');
}
}
// Tests to see if /users/<userId> has any data.
function checkIfUserExists(userId) {
var usersRef = new Firebase(USERS_LOCATION);
usersRef.child(userId).once('value', function(snapshot) {
var exists = (snapshot.val() !== null);
userExistsCallback(userId, exists);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment