So I want to return a value from a subscribe :
export class UserHomePage implements OnInit {
uid: string;
...
constructor(
private afAuth: AngularFireAuth,
private afs: AngularFireStore,
...
) {}
getAuth() {
return this.afAuth.authState.subscribe(auth => { <= // It's ok
this.uid = auth.uid;
console.log('ID : ' + this.uid);
});
}
I try to use "this.uid" in other function but the data is "undefined" :
example() { <= // Problem !
console.log(this.uid);
}
I just find an other subject on this forum here but i dont understand the observable way.
Could u explain to me how to return this data for the next functions.
Thanks (and sorry for my english)!
My best guess is that your problem here is that observables are asynchronous, hence this.uid
returns undefined
if example()
is triggered before your subscription to this.afAuth.authState
returns any value.
The simplest thing you can do is to call example()
from the subscription itself. Something like this should work:
getAuth() {
this.afAuth.authState.subscribe(auth => {
this.uid = auth.uid;
this.example();
});
}