Hello how to return correct value

Hello how to return correct value
0

#1

hi i coded lately a bit and was having a problem if i have this function

  getUser(){
    firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
     
      
      // User is signed in.
    } else {
      
      // No user is signed in.
    }
  })
  
   }

so how do i get the user object down to function foot
here`

    firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
     
      
      // User is signed in.
    } else {
      
      // No user is signed in.
    }
  }) ===>> return user
  
   }``

#2

I am having trouble understanding what you are asking. Can you give an example based on actual data which would show what you expect to see at some place in the code?


#3
 getUser(){
    firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
     console.log(user.email)==> [email protected]
      
      // User is signed in.
    } else {
      
      // No user is signed in.
    }
  })
  console.log(user)==> user is undefined
   }

i know its odd question but function only knows what user is in the call back brackets
how to transfer the user object to outer scope best way?


#4

You will have to do whatever you are wanting to do with user inside the callback function. I assume onAuthStateChanged is asynchronous, so it does not prevent other code from running while it waits for the state to change. That is why you get undefined when trying to immediately console.log the user info outside the callback function. You can always call other functions to do specific things. This is the nature of using async methods using callback functions.

getUser(){
  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      console.log(user.email); // displays `[email protected]`
       doSomethingWithUser(user);
       // User is signed in.
    } else {
      // No user is signed in.
    }
    doSomethingWithUser(user);
  });

  function doSomethingWithUser(user) {
    console.log(user.email) // displays '[email protected]'
  }
}

#5

ye i cant figure it out dawson, but I keep trying its function for seeing if user is logged in / user object in firebase my favourite =)


#6

Randell I have another question that is I wonder like this im trying to code a boolean in an promise chain but typescript js doesnt allow it how can I go about it? the code i want to add is in the result promise. {{ this.olga=true}}


olga:bollean=false;
login(user, pass){
  
  this.apple11.logIn(user,pass).then(res=>console.log(res.user),{{this.olga=true}}).catch(err=>this.olgo.push(err.code))

 
}