0

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.

Community
  • 1
  • 1
dbm
  • 35
  • 1
  • 6

1 Answers1

0

You can declare a variable outside the api call and assign the data value to it once it's available

// Declare a variable here.
let allData;

// Get API data
alpha.data.daily('msft', 'compact')
    .then(data => {allData = alpha.util.polish(data))['data']});

If you are using React, a common place to store this data is state.

 constructor(props) {
    super(props);
    this.state = {data: []};
 }

Then use setState to set when the promise from the API call is resolved:

// Get API data
alpha.data.daily('msft', 'compact')
    .then(data => {this.setState(data: alpha.util.polish(data))['data']}));
Claire Lin
  • 2,372
  • 8
  • 13
  • Thanks. I implemented the changes, however I still get `undefined` if I try to log the `allData` variable. I've updated the main post with an example of what the data looks like when I only log to the console if that is of any help. Thanks for including how to implement it in the React component too! – dbm Nov 26 '19 at 04:23
  • How is your code structured? Can you update the code with the changes? – Claire Lin Nov 26 '19 at 04:26
  • I've updated the main post with the code, it's really just those couple of lines trying to figure out how to pull the data. However, I think I figured it out as it's probably due to the ansynchronous nature of javascript (?) so the `console.log()` gets called before the data is ready? I also tried chaining another `.then(console.log(polishedData));` at the end but it doesn't work. Maybe logging to the console isn't appropriate, I'm just very used to using `print` statements to debug. – dbm Nov 26 '19 at 04:41
  • @dbm You are correct that the `console.log` is executed before the promise is resolved, which means the log is printed before ` polishedData` gets its final value. If you use `.then(() => console.log(polishedData)`, it should log properly. – Claire Lin Nov 26 '19 at 04:49
  • Yes, that did the trick. Thanks! I'll fiddle around a bit more with this. – dbm Nov 26 '19 at 04:52