Angular 2:在订阅数组中添加数据导致多次添加

时间:2017-07-02 09:05:37

标签: firebase firebase-realtime-database firebase-authentication angularfire2

我遇到一种奇怪的情况,我用firebase数据订阅了firebaseObservable。

代码:

login() {
    this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()).then((success)=>{
        console.log("Logged in user is ",this.user);
        this.user.subscribe(signedInUser => {
        console.log(signedInUser,"User Data")

        this.userObject = {
            id : signedInUser.uid,
            userName:signedInUser.displayName
        }
        this.userData.subscribe(userData=>{
          console.log("User Dt",userData);
            userData.forEach(user=>{
             console.log("In user parse",user.id,"    ",signedInUser.uid )
             if(user.id == signedInUser.uid )  {
               this.count++;
               console.log("ins the signed user check ",this.count);
             }
            })

      });

        if(this.count ==0){
             console.log("count is 0");
             this.userEdit.push(this.userObject);
           }else {
             console.log("user already exist.");
           }
           this.count=0;
           this.router.navigateByUrl('/');
    })
    })
  }

使用此代码,我希望仅在尚未添加用户的情况下将用户添加到我的firebase数据库。问题是,当我添加用户时,它会被多次添加。有人可以告诉我这有什么问题吗?

0 个答案:

没有答案