Hello Earthlings
I have an interesting problem that I would very much appreciate help with. You what has gone and happened is this, I have created a method inside my lovely Class, the method’s job is to read an online database, Firebase’s CloudFireStore - yeah I’m also expecting a flaming phoenix to fly across the horizon every time I mention Firebase
The method is getting the data as expected but, can write it to console, there is trouble in paradise when I try to reuse the variable outside of the “if-else” statement I am using to confirm the data, is this weird or what!!! I bet it’s what
fireNewStudent(){
let lastStudentNum = db.collection("control").doc("lastStudentNum");
let StudentNum;
lastStudentNum.get().then(function(doc){
if (doc.exists){
currentStudentNum = doc.data();
StudentNum = currentStudentNum.lastStudentNum;
console.log(StudentNum);
// Prints expected result
} else{
// doc.data will be undefined in this case
console.log("Error getting current student number");
}
}).catch(function (error) {
console.log("Error getting document: ", error)
});
document.write(JSON.stringify(currentStudentNum));
//Prints out an empty object => {}
}
Thanks a bunch for having a look and hope to hear from you soon