I´m new with Firebase. I´m developing an angular + firebase app where after user has been registered with email + password, he can save his username after submit a profile form.
Before it, I want to know if any user in the application has the username submitted. This aproch doesnt work because scope.used is always false:
scope.used = false;
scope.updateProfile = function(profile) {
var ref = new Firebase(FIREBASE_URL + '/users');
ref.orderByChild("username").equalTo(profile.username).on("child_added", function(snapshot) {
if (currentUser != snapshot.key()) {
scope.used = true;
}
});
if (scope.used) {
console.log('username already exists');
}
};
Is there another way to accomplish it?
EDIT (I answer to myself)
This approach works:
scope.used = false;
var userRef = new Firebase(FIREBASE_URL + '/users/' + currentUser);
scope.updateProfile = function(profile) {
var ref = new Firebase(FIREBASE_URL + '/users');
ref.orderByChild("username").equalTo(profile.username).on("child_added", function(snapshot) {
if (currentUser != snapshot.key()) {
scope.used = true;
}
});
ref.orderByChild("username").equalTo(profile.username).once("value", function(snap) {
//console.log("initial data loaded!", Object.keys(snap.val()).length === count);
if (scope.used) {
console.log('username already exists');
scope.used = false;
}else{
console.log('username doesnt exists, update it');
userRef.child('username').set(profile.username);
}
});
};