0

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"?

eylay
  • 1,712
  • 4
  • 30
  • 54
malachi54
  • 67
  • 1
  • 1
  • 5
  • It appears that you have to use a callback within getDatetimesOfPackages because just returning a map-object within a callback of getJSON is no use. – Blauharley Dec 18 '15 at 23:06
  • @Blauharley i edited my question. i read the "solution" but i mean i have an returned object. it is just "undefined". so i think the return part itself should be ok or not? – malachi54 Dec 18 '15 at 23:18
  • *"So why this problem of "undefined"?"* Because there is no `return` statement in `getDatetimesOfPackages`. The `return` statement inside the nested function is irrelevant. – Felix Kling Dec 19 '15 at 01:06

0 Answers0