如何在firebase数据库中添加尚不存在的用户?

时间:2017-06-07 07:56:04

标签: angularjs firebase firebase-realtime-database angularfire

    export class UsersComponent implements OnInit {

  users: any;
  constructor(private fs: FirebaseService, public afAuth: AngularFireAuth ) {


  // console.log(  this.afAuth.auth.currentUser.email);
      // this.fs.addUsers();
  }

  ngOnInit() {
    this.fs.getUsers().subscribe(users => {
      this.users = users;
          if (this.users[0].email === this.afAuth.auth.currentUser.email ) {
            console.log("this user already exists");
          }
          else if (this.users[0].email !== this.afAuth.auth.currentUser.email) {
             this.fs.addUsers();

          }
          console.log(this.users[0].email === this.afAuth.auth.currentUser.email);
          console.log(this.users[0].email);
          console.log(this.afAuth.auth.currentUser.email);
    });

  }

}

这是我的代码;我希望当登录用户时将其添加到用户列表中(存储在firebase数据库中);但是我想检查用户是否已经存在于列表中。因此我写了这个函数。然而它被反复调用并且不会停止。我的问题是:如何更改功能,使其不进入无限循环?

ps我使用angularfire 4.0.0 / angular 4.1.3 / firebase 4.0.0

编辑:

  getUsers() {
this.users = this.afd.list('/users') as FirebaseListObservable<any[]>
return this.users;

}

0 个答案:

没有答案