I want to access Object with data from API response.
Presently, the response in the console looks like this:
But I want to get this data:
This is browser script. Not NODE
My entire code...
const input = document.querySelector('input[type="file"]');
function getData(symbol) {
return fetch(
`https://cors-anywhere.herokuapp.com/` +
`https://eodhistoricaldata.com/api/fundamentals/${symbol}?api_token=***************`,
{
method: "get",
}
)
.then((data) => {
if (!data.ok) {
throw new Error("Symbol is 'empty'");
}
(data) => data.json();
console.log(data);
return data;
})
.catch((error) => {
throw error;
});
}
input.addEventListener(
"change",
function (e) {
const reader = new FileReader();
reader.onload = async function () {
const symbolsArr = reader.result.split("
").map((str) => str.trim());
let json_file = await getData(symbolsArr[0]);
saveFile(json_file, "json.txt", "text/plain");
symbolsArr.forEach((element) => {});
};
reader.readAsText(input.files[0]);
},
false
);
function saveFile(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
I want to save the Object from the response to a json_file
variable and then execute the saveFile
method to save the file locally.
question from:
https://stackoverflow.com/questions/65937556/how-to-get-access-object-from-api-response 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…