Q1: I am encountering an issue where both callbacks are working, but instead of 2 results, I am getting 4 results with onderhoud+undefined and macro+undefined. How can I resolve this?
Q2: Is there a way for me to pass all the variables from loadMacro in the callback and then use them separately within the callback?
function loadOnderhoud(getData) {
var username = window.sessionStorage.getItem("huidigeGebruiker");
var url = "restservices/gebruiker?Q1=" + username;
$.ajax({
url : url,
method : "GET",
beforeSend : function(xhr) {
var token = window.sessionStorage.getItem("sessionToken");
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
success : function(data) {
var onderhoud;
$(data).each(function (index) {
if (this.geslacht == "m"){
onderhoud = (66 + (13.7 * this.gewicht) + (5 * (this.lengte*100)) - (6.8 * this.leeftijd)) * this.activiteit;
}
else if (this.geslacht == "v"){
onderhoud = (655 + (9.6 * this.gewicht) + (1.8 * (this.lengte*100)) - (4.7 * this.leeftijd)) * this.activiteit;
}
});
getData(onderhoud);
},
});
}
// Load ingredients from JSON test file
function loadMacro(getData) {
var username = window.sessionStorage.getItem("huidigeGebruiker");
var datum = document.getElementById("datepicker").value;
var url = "restservices/ingredients?Q1=" + username + "&Q2=" + datum;
$.ajax({
url : url,
method : "GET",
async: false,
beforeSend : function(xhr) {
var token = window.sessionStorage.getItem("sessionToken");
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
},
success : function(data) {
var totalCal=0;
var totalVet=0;
var totalVv=0;
var totalEiwit=0;
var totalKh=0;
var totalVezels=0;
var totalZout=0;
$(data).each(function (index) {
totalCal = (this.hoeveelheid * this.calorieen) / 100;
totalVet = (this.hoeveelheid * this.vet) / 100;
totalVv = (this.hoeveelheid * this.verzadigd_vet) / 100;
totalEiwit = (this.hoeveelheid * this.eiwit) / 100;
totalKh = (this.hoeveelheid * this.koolhydraten) / 100;
totalVezels = (this.hoeveelheid * this.vezels) / 100;
totalZout = (this.hoeveelheid * this.zout) / 100;
});
getData(totalCal);
},
});
}
function getData(data, dataa)
{
var onderhoud = data;
var macro = dataa;
console.log(onderhoud, macro);
}
loadOnderhoud(getData);
loadMacro(getData);