I need some help here...
In the first line of my getDiagrammData()
function i call
getDatetimesOfPackages()
and getDatetimesOfPackages()
uses $.getJSON()
.
So i am aware of that i have to return my map in the callback function of getJSON().
But when i want to use the variable in my getDiagrammData()
function with:
m.forEach()
i get this:
Uncaught TypeError: Cannot read property 'forEach' of undefined
So here is my code:
function getDiagrammData() {
var m = getDatetimesOfPackages();
console.log(m);
var data = new google.visualization.DataTable();
data.addColumn('string', '');
data.addColumn('number', 'Wochentag');
data.addColumn('number', 'Uhrzeit');
data.addColumn('string', '');
data.addColumn('number', 'Anzahl');
m.forEach(function (value, key) {
data.addRow('', key.wochentag, key.uhrzeit, '', value);
}, m);
m.forEach(function (value, key) {
console.log("wochentag: " + key.wochentag + " uhrzeit: " + key.uhrzeit + " anzahl: " + value);
}, m);
console.log("groesse: " + m.size);
return data;
}
function getDatetimesOfPackages() {
var url;
var m = new Map();
url = document.URL + 'getDatetimes/';
$.getJSON(url, function (db_data) {
console.log("Datetimes refreshed");
$.each(db_data, function (i, item) {
var date = new Date(db_data[i].empfangen);
var day = ((date.getDay() !== 0) ? date.getDay() + 1 : 7);
var hr = date.getHours();
var an = new Ankunft(day, hr);
console.log(an.wochentag, an.uhrzeit);
if (m.has(an)) {
var anzahl = m.get(an);
anzahl++;
m.set(an, anzahl);
} else {
m.set(an, 1);
}
});
console.log("size: " + m.size);
return m;
});
}
The last console.log("size: " + m.size);
is the right size.
And then i call return m;
.
So what should i do when i get the Map m in the function "getDiagrammData()"? Thanks in advance.
edit:
maybe i should edit, that getDiagrammData()
itself is called from another
function. So i cannot just call that in a callback.
Could someone explain why i cannot just return the map?
I mean at that point it is "ready". So why this problem of
"undefined"?