查询Firebase中唯一的用户名

时间:2016-02-25 19:40:22

标签: angularjs firebase

我是Firebase的新手。我正在开发一个angular + firebase应用程序,在用户注册了电子邮件+密码之后,他可以在提交个人资料表单后保存他的用户名。 在此之前,我想知道应用程序中的任何用户是否提交了用户名。这个aproch不起作用,因为scope.used总是错误的:        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('用户名已存在');             }         }; 还有另一种方法可以实现吗? 编辑(我自己回答) 这种方法有效:         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('用户名已存在');                     scope.used = false;                 }其他{                     console.log('用户名不存在,更新');                     userRef.child( '用户名')设置(profile.username)。                 }             });         };

0 个答案:

没有答案