I have been utilizing an API to fetch data on various stocks, and I am attempting to include a column named "symbol" with the query values using the function insertColumn
. However, I am encountering an error message that says
(node:15732) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'result' of undefined
.
I have made an attempt at modifying let result
to this.result
and then executing insertColumn.call(this)
, but unfortunately, the same error persists. Initially, I assumed it could be related to a closure error, but now I'm uncertain.
import ObjectsToCsv from 'objects-to-csv';
import yahooFinance from 'yahoo-finance2';
async function api(){
const query = 'TSLA';
const queryOptions = { period1: '2021-08-06', interval: "1d"};
let result = await yahooFinance.historical(query, queryOptions);
function insertColumn() {
for (var i = 0; i < this.result.length; i++) {
this.result[i].push({symbol: query});
}
};
insertColumn();
console.log(result);
(async () => {
const csv = new ObjectsToCsv(result);
await csv.toDisk('C:/Users/Rafael Oliveira/Desktop/teste/test.csv');
})();
};
api();
The current output looks something like this:
[
{
date: 2021-08-06T00:00:00.000Z,
open: 711.900024,
high: 716.330017,
low: 697.630005,
close: 699.099976,
adjClose: 699.099976,
volume: 15576200
},...
However, my desired outcome is as follows:
[
{
date: 2021-08-06T00:00:00.000Z,
open: 711.900024,
high: 716.330017,
low: 697.630005,
close: 699.099976,
adjClose: 699.099976,
volume: 15576200
symbol: 'TSLA' //this value will vary based on changes in the query part
},