Home » Javascript » Retrieving values from an API call | Wix

Retrieving values from an API call | Wix

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am using an API to GET the currency rates. The base & output currency are being entered by the user.

This is my Backend:

    import {fetch} from 'wix-fetch';  


export function getCurrency(currency,baseCurrency) {
    const url = 'https://api.fixer.io/latest?base=';

    let fullUrl = url + baseCurrency + '&symbols=' + currency;

    return fetch(fullUrl, {method: 'get'})
      .then(response => response.json())
      .then(json => json.rates[currency].toString());
}

And This is my Page Code:

    import {getCurrentTemp, getCurrency} from 'backend/ArenaApi';

export function button1_click(event, $w) {
    getCurrency($w("#currencysymbol").value,$w("#baseCurrency").value)
  .then(rates => $w("#currencyrate").text = "1 " + ($w("#baseCurrency").value) + " = " + rates + " " + ($w("#currencysymbol").value));
    $w("#currencyrate").show();
}

From what I understand my Backend is getting this data:

"base":"USD","date":"2018-05-22","rates":{"EUR":0.84789}

and it is retrieving the “rates” from this string via the following line in the backend:

.then(json => json.rates[currency].toString());

I want to know how can I retrieve multiple values from a single string ? For this instance: “rates” “base” “date” and then inject it into a text element in the page like:

.then(rates => $w("#currencyrate").text = "1 " + ($w("#baseCurrency").value) + " = " + rates + " " + ($w("#currencysymbol").value));
Answers: