0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
raulabad
  • 384
  • 2
  • 9
  • 2
    I mean.. getRole doesn’t actually return anything. Just return the promise..? – MikeOne Jun 01 '21 at 22:22
  • @MikeOne thank you for your information, although the documentation seemed very technical, however, it was very complete, it helped me to understand certain things, but I found this in the Hispanic community (https://es.stackoverflow.com/questions/110242/como-hacer-correctamente-una-funcion-que-retorne-una-promesa-con-ionic), it helped me a lot, however thank you very much for your prompt responses and your support, I solved my problem by the way, thank you very much seriously. – raulabad Jun 02 '21 at 15:29

1 Answers1

0

Try to make your getRole function async.