I want to get user data from firestore, but functions don't behave as I want.
When I run the init function I got a response in the console like: " in init", "after", "null", "user is active", but I want to receive a response like: "user is active", "in init", user data instead null," after";
I know it is probably simple, but I am sitting all day on this problem.
What should I do to get data in this order?
init()
async function init () {
await userIsActive()
console.log("in init");
await getDaySpending();
console.log("after") */
}
function userIsActive(){
firebaseApp.auth().onAuthStateChanged( user=>{
if(user){
console.log("user is active");
return true
}else{
console.log("no user");
return false
}
})
}
function getDaySpending(period = '7-2020') {
const user = firebase.auth().currentUser;
console.log(user) //I got null instead user data
if(!user) return;
const uid = user.uid;
const snap = await firebase.firestore().collection(uid).doc(period).get();
if(snap.exists) {
return snap.data().spending;
} else {
// handle the case where there was no data
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…