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();
});
});
})();