When I call the function from somewhere else, it does not return the desired value, the problem is that it returns an undefined value before executing the function, it works I have it stored in a service.
login.page.ts:
ngOnInit(){
console.log(this.auth.getRole());
}
auth-admin.service.ts:
First Attempt
getRole() {
this.afAuth.onAuthStateChanged(user => {
if (user) {
this.afs.firestore.doc(`Core/Usuarios/Admin/${user.uid}`)
.get()
.then(userProfileSnapshot => {
let isAdmin = userProfileSnapshot.data().udbid.role;
return isAdmin;
})
}
});
}
CONSOLE LOG
undefined
In my second attempt I put a console message before returning the value, which gave me in my console the desired value but it did it some time after giving me an undefined value, but the function never applied the return.
Second Attempt
getRole() {
this.afAuth.onAuthStateChanged(user => {
if (user) {
this.afs.firestore.doc(`Core/Usuarios/Admin/${user.uid}`)
.get()
.then(userProfileSnapshot => {
let isAdmin = userProfileSnapshot.data().udbid.role;
console.log(isAdmin)
return isAdmin;
})
}
});
}
CONSOLE LOG:
undefined
Admin