Created
December 18, 2012 00:54
-
-
Save anantn/4323949 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 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); | |
}); | |
} |
if you want to listen on a node. You can change the implementation to the snippet below.
const checkIfUserExists = (userId) => {
const usersRef = new Firebase(USERS_LOCATION);
usersRef.child(userId).on('value', (snap) => {
const exists = (snap.val() !== null);
userExistsCallback(userId, exists);
});
}
didn't know about the once() method. thanks 👍
Can you do this in swift?
Nice!
+2
I am making chat application using java, "https://github.com/Abdul007Malik/Conversa.git", I am confused and cannot progress further help me if you can, my questions:
- if I know the auth.getUID of others can i modify there data or firebase have different mechanism to check authentication.
- how can I send the invitation(inviting to join the group) to friends if i know there phone numbers but dont know there particular userid.
Did you find any solution for this. I am facing same issue where I know email id but not user id
Can you give us python examples??
Can you give us python examples??
For python example
https://stackoverflow.com/questions/59253867/how-to-check-if-a-child-in-firebase-database-exists-using-django/60844998#60844998
thank's man, very helpful =)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The crux: var exists = (snapshot.val() !== null);