In my Ionic 2 application using Angular Fire 2, I am trying to get Current User Id
in constructor
. However, I noticed that the constructor
fires twice and though it show capture logged in user's Current ID
the second time, the first time it gives an error, like the currentUser
object is not defined. Below is the constructor
code I am using;
constructor(private afAuth: AngularFireAuth, private afDatabase: AngularFireDatabase, private auth: AuthProvider) {
console.log('Hello AnonymousTask Provider');
this.userId = this.afAuth.auth.currentUser.uid;
console.log(this.userId)
this.taskList = this.afDatabase.list(`/tasks/${this.afAuth.auth.currentUser.uid}/anTasks`);
}
Below is the console snapshot showing the error message.
I couldn't really understand why the constructor
is being fired twice. I understand that on the first time it fired, the Current User
object might not be ready.
I tried to use await
but the method is synchronous. Any suggestions as to how I could fix this, or what worked for you?