0

I've just started to learn react, and I'm very new to it. Right now I was trying to call some data from free currency conversion API. When I'm calling the api, it returns something like this:

{
    "AUD_NZD": 1.089788
}

or

{
    "USD_AUD": 1.404502
}

As you can see, the very top level of the returned data is changing as I enter different money type. And here is my code to fetch the data:

const API_INFO = primary + '_' + secondary;
const API_CALL = await fetch(`https://free.currencyconverterapi.com/api/v6/convert?q=${API_INFO}&compact=ultra`);
const API_DATA = await API_CALL.json();
const result = API_DATA.API_INFO;

But the const result is actually returning undefined, as API.DATA.API_INFO does not exist in the returned data.

Is there a way to use dynamic values of API_INFO to actually fetch the conversion rate only?

Cheers.

J Kim
  • 13
  • 3
  • 1
    See the linked question's answers for details. Basically: `const result = API_DATA[API_INFO];`. (Side note: In JavaScript, using ALL CAPS for variables is extremely uncommon [even when they're local constants].) – T.J. Crowder Sep 10 '18 at 10:11
  • 1
    Sweet, Its working! Thanks for the fast response. And I will fix the caps. Cheers. – J Kim Sep 10 '18 at 10:21

0 Answers0