I'm new to javascript, and I'm trying to do some API calls of stock prices in React/Node.js using this package. I've figured out how to get the data I need, but can't seem to assign it to an object (?) to transform the data into an usable format.
My code looks like this:
const alpha = require('alphavantage')({ key: 'myapikey' });
// Declare variable
let polishedData;
// Get API data
alpha.data.daily('msft', 'compact')
.then(data => {polishedData = alpha.util.polish(data)['data']});
console.log(polishedData); // returns undefined
The api returns a promise with the data in json format:
{
'2019-11-25T00:00:00.000Z': {
open: '262.7100',
high: '266.4400',
low: '262.5200',
close: '266.3700',
volume: '19242309'
},
...
}
Logging to the console displays the data nicely so I know at least that part works.
I've tried just adding var somedata = in front or just chaining .then(data => { return data }); (without using console.log() obviously) but it doesn't work.
How would I go about assigning this to a variable so I can manipulate the data into an usable format for my graph package? Sorry if this is a stupid question but I'm at a loss.
Update: Added the changes suggested below.