2

I am attempting to get a value from an array with a for loop and insert it into a JSON url. Here is the code.

myConnector.getData = function(table, doneCallback) {
    datasetWebId.forEach(function(v) {
        var webId = (v);
        $.getJSON("https://www.twdc/api/" + webId + "/recorded?starttime=*-3d&endtime=*", function(resp) {

            tableData = [];
            resp.Items.forEach(function(item) {
                item.Items.forEach(function(subItem) {
                    tableData.push({
                        'name': item.Name,
                        'Timestamp': subItem.Timestamp,
                        'Value': subItem.Value,
                        'Path': item.Path
                    });
                });
            });

        });
        table.appendRows(tableData);
        doneCallback();
    });

};

When it runs it hits the first loop and just skips the rest. There is values in datasetWebId array, so that is no the issue. Can someone spot something out of place?

BrTkCa
  • 4,703
  • 3
  • 24
  • 45
llerdal
  • 377
  • 1
  • 4
  • 16
  • 1
    please show your json – Pradyut Manna Oct 03 '16 at 22:30
  • Json isn't the problem there is something when with the loop and how Webid is put into the irk. Because it works without that. @PradyutManna – llerdal Oct 03 '16 at 22:33
  • any error in console? @llerdal – Pradyut Manna Oct 03 '16 at 22:40
  • 1
    I think this is another question on [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323), like an AJAX-call (or multiple). – Thomas Oct 03 '16 at 22:40
  • There is so much that could be to blame for this. I suspect `CORS` to be the perp. – Derek Pollard Oct 03 '16 at 22:42
  • If works without Webid, what value has Webid and what value is passed when you don't use it? – BrTkCa Oct 03 '16 at 22:44
  • Happy to reopen this if you can edit it to add the JSON as requested, though it looks like you have asked another question about this shortly after this one. – halfer Oct 06 '16 at 20:45

2 Answers2

2

You called doneCallback at the end of the first item of datasetWebId array, which is why it never have another iteration.

myConnector.getData = function(table, doneCallback) {
    datasetWebId.forEach(function(v) {
        var webId = (v);
        $.getJSON("https://www.twdc/api/" + webId + "/recorded?starttime=*-3d&endtime=*", function(resp) {

        tableData = [];
        resp.Items.forEach(function(item) {
            item.Items.forEach(function(subItem) {
                tableData.push({
                    'name': item.Name,
                    'Timestamp': subItem.Timestamp,
                    'Value': subItem.Value,
                    'Path': item.Path
                });
            });
        });

    });
    table.appendRows(tableData);
  });
  doneCallback(); //This has been moved.
};
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Hin Fan Chan
  • 1,543
  • 10
  • 11
0

There are a few things going on here. First, we have no idea what "datasetWebId" is. This is your array that you're iterating through to send each AJAX request, no doubt, but if it's quitting after one you should console.log the length of this before it runs, to make sure it actually has data to iterate through.

Second. You have 3 (!) nested for-loops. It's not at all shocking there's a weird data leaking issue, as this is one of the major symptoms of a bit too much over-nesting. Not to worry, we've all been there. Throw all of those in separate functions you can test independently, and start running them one at a time. And, don't do anything until you make sure the symbol fire-response of the AJAX is working properly.

Okay, you got this. If you can, post more info on that array, and I'd be happy to send you through a cleaned up interpretation.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jack Connor
  • 111
  • 3