I know it is a scope problem. For some reason I can't get the function caculateValue to return the value of "oplossing". console.log (oplossing + "oplossing-0"); console.log (oplossing + "oplossing-1"); and can read the values. It stops at: console.log (oplossing + "oplossing-2"); From here I get an error. Who knows a solution for this.
export function caculateValue(nieuwVerbruik, nummer) {
let database = firebase.database();
let monthRef = database.ref("eindemaand");
monthRef.orderByChild("dateID").limitToLast(1).once('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
// this is the whole object
console.log(snapshot.val());
//these are the various property values
let dateID = childSnapshot.child("dateID").val();
let oldmeterstandVerEL = childSnapshot.child("meterstandVerEL").val();
let oldmeterstandVerEH = childSnapshot.child("meterstandVerEH").val();
let oldmeterstandTerEL = childSnapshot.child("meterstandTerEL").val();
let oldmeterstandTerEH = childSnapshot.child("meterstandTerEH").val();
let oldmeterstandVerGas = childSnapshot.child("meterstandVerGas").val();
let oldmeterstandVerWat = childSnapshot.child("meterstandVerWat").val();
let oldmeterstandZonPan = childSnapshot.child("meterstandZonPan").val();
let old = [oldmeterstandVerEL, oldmeterstandVerEH, oldmeterstandTerEL, oldmeterstandTerEH, oldmeterstandVerGas, oldmeterstandVerWat, oldmeterstandZonPan];
window.oplossing = nieuwVerbruik - old[nummer];
//globalThis.oplossing = nieuwVerbruik - old[nummer];
// this is going well
console.log(nummer);
// this is going well
console.log(nieuwVerbruik);
// this is going well
console.log(oplossing + " oplossing-0");
});
//this is going well
console.log(oplossing + " oplossing-1");
});
// At 'window.oplossing' and at 'globalThis.oplossing' it gives the error message 'Uncaught refferenceError: solution not Defined'
console.log(oplossing + " oplossing-2");
}