I have a problem.
docRef.get().then(...)
is a function which works asynchronously. Since it returns immediately, username will not be set. How could I get the value and set it to the variable username
?
var username;
var kontostand;
var ticket;
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
document.getElementById("user_div").style.display = "block";
document.getElementById("login_div").style.display = "none";
var docRef = db.collection("users").doc(user.uid);
// Get the data from the user
docRef.get().then(function (doc) {
if (doc.exists) {
//console.log("Document data:", doc.data());
kontostand = doc.data().kontostand;
ticket = doc.data().ticket;
username = doc.data().username;
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function (error) {
console.log("Error getting document:", error);
});
var user = firebase.auth().currentUser;
if (user != null) {
document.getElementById("user_para").innerHTML = "Welcome User : " + username;
}