What is a way to check if a user exists at a firebase location? Would you use .child()
somehow? A variable would store the user name entered:
user name:
var user = "test";
firebase returned object:
snapshot { '
-JruhOP3M496hEAKn6':
{
email: 'chaselester.cl@gmail.com',
password: 'kimberlyn#1',
username: 'chipe'
},
'-Ju8J72xo3UpSIwEYw': {
email: 'test',
password: 'test',
username: 'test' },
'-Ju8J9m_DC18irjWep': {
email: 'one',
password: 'three',
username: 'two'
}
}
Update: code from comment
It seems if the user does not exist firebaseRef.push
calls firebaseRef.orderByChild
and the whole function runs again because in the log it logs "User does not exist" then right after it logs "User Exists" and my res.json is called twice and I get an app crash with the comment "Can't set headers after they are sent". How can I push to firebase if the user does not exist without having it re evaluate the firebaseRef.orderByChild
?
Here is the full function I am doing in my server.js:
firebaseRef.orderByChild('username').equalTo(user).on('value', function(snapshot) {
if (snapshot.hasChildren()) {
console.log('User exists');
res.json(true);
}else if(!snapshot.hasChildren()){
console.log('User does not exist');
firebaseRef.push(req.body);
res.json(false);
}
});