Forked from cooljith91112/firebase_detect_data.js
Created
September 14, 2016 17:56
-
-
Save coryarmbrecht/b2b10a1a0ec2c181ae66f389dfcb56a4 to your computer and use it in GitHub Desktop.
Firebase: Detecting if data exists. This snippet detects if a user ID is already taken
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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