0

I am working on this code to grab data from a website and put the data into a table. The XMLhttprequest works and I get the data back and can store it. However as soon as I add this for loop of x=1999 the response is null. I know I have not added the "x" value to each xml request to iterate through as I'm just trying to get it to work on first iteration first.

(function () {
  // Create the connector object
  var myConnector = tableau.makeConnector();

  // Define the schema
  myConnector.getSchema = function (schemaCallback) {
    var cols = [
      {
        id: "Currency",
        dataType: tableau.dataTypeEnum.string,
      },
      {
        id: "Price",
        alias: "Price",
        dataType: tableau.dataTypeEnum.float,
      },
      {
        id: "Year",
        alias: "Year",
        dataType: tableau.dataTypeEnum.float,
      },
      {
        id: "Base",
        alias: "Base",
        dataType: tableau.dataTypeEnum.string,
      },
    ];

    var tableSchema = {
      id: "EXratesFeed",
      alias: "Exchange Rates latest feed",
      columns: cols,
    };

    schemaCallback([tableSchema]);
  };

  // Download the data
  myConnector.getData = function (table, doneCallback) {
    //$.getJSON("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_week.geojson", function(resp) {

    tableData = [];
    //loop through all dates
    //var x = 1999;
    //for (var x = 1999; x<2022; x++) {

    var requestURL = "https://api.exchangerate.host/latest";
    var request = new XMLHttpRequest();
    request.open("GET", requestURL);
    request.responseType = "json";
    request.send();

    request.onload = function (resp) {
      var response = request.response;
      console.log(response);

      var feat = response.rates;

      //var year = x;

      // Iterate over the JSsON object
      var count = Object.keys(feat).length;
      for (var i = 0, len = count; i < len; i++) {
        var currencyAbr = Object.keys(feat)[i];
        var currencyPrice = Object.values(feat)[i];
        tableData.push({
          Currency: currencyAbr,
          Price: currencyPrice,
          Base: response.base,
          //  "Year": x,
          //"mag": feat[i].properties.mag,
          //"title": feat[i].properties.title,
          //"location": feat[i].geometry
        });
      }
    };
    table.appendRows(tableData);

    doneCallback();
    //};
  };

  tableau.registerConnector(myConnector);

  // Create event listeners for when the user submits the form
  $(document).ready(function () {
    $("#submitButton").click(function () {
      tableau.connectionName = "Exchange rate feed";
      tableau.submit();
    });
  });
})();
Behemoth
  • 5,389
  • 4
  • 16
  • 40
Dan_68
  • 3
  • 1
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Jun 19 '21 at 10:35
  • [JavaScript closure inside loops – simple practical example](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Andreas Jun 19 '21 at 10:36
  • Why is the request in the loop? You only use `x` for the result. Nothing from the request or response uses `x` o.O – Andreas Jun 19 '21 at 10:37

1 Answers1

0

Use let instead of var for your request and it will be scoped to the block:

 for (var x = 1999; x<2022; x++) {
 ....
 let request = new XMLHttpRequest();
 ....
 }
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39