问题
I have small function and it works when I look in console: returns user.uid in console but I want to return uid to use in other function
newUserUid() {
return firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log(user.uid);
}
});
}
I want to function return uid like this: ewfwe3423sdfsdakmasd
回答1:
You need to subscribe
to the auth state. You are using the JavaScript
SDK inside an Angular 2
app, use the AngularFire2
SDK instead.
import { AngularFireAuth } from 'angularfire2/auth';
...
constructor(public af: AngularFireAuth) {}
...
someOtherFunction() {
newUserUid().subscribe(auth => {
// Now use value
if (auth) {
console.log(auth.uid);
} else {
// logged out
}
});
}
newUserUid() {
return this.af.auth;
}
Refs: Here and here
来源:https://stackoverflow.com/questions/44795636/return-user-uid-onauthstatechanged