检查数据库 firebase angularfire2 中是否存在用户名

Check username exist in database firebase angularfire2

注册和登录后,我要求用户创建一个 profile.Right 我需要检查用户输入是否存在于数据库中,如果不创建 profile.As 你会看到数据 [0][4] = 数据库中的用户名。

使用 for 循环 im 遍历所有配置文件用户名以查找用户名存在或 not.right 现在它创建配置文件,即使用户名存在。

谢谢

https://image.prntscr.com/image/qDttSENCRqaOncLmpXNuhA.png

createProfile(){

    this.allData=this.db.list(`/profile/`).valueChanges();
    this.allData.take(1).subscribe(data => {
      let data = data.map(this.getObjectWithoutKnowingKey);
      console.log(data);
      console.log(data[0][4]);
       console.log(data.length);
console.log(this.username);


for(var i=0;0<data.length;i++){

if(data[i][4] === this.username){
  console.log("username already exist");
return;

}

}
});

//if username exist dont execute this code below.

this.fire.authState.take(1).subscribe(auth => {


this.db.object(`profile/${auth.uid}/`).set({
username: this.username,
msgnumber: 0,
asd: 0,
avatar: this.avatar


});
this.db.object(`profile/${auth.uid}/inventory/`).set({

test: 1


}).then(() => this.navCtrl.setRoot(TabsPage));



});






}

通过代码,我可以看到您正在迭代数据。所以这将只需要你而不是尝试使用 firebase 的 equalsTo 方法。 Check it out.