Fetch API Get Elements Response Data

import {fetch} from 'wix-fetch';

fetch("https://currency-converter5.p.rapidapi.com/currency/convert?format=json&from=USD&to=TRY&amount=1", {
 "method": "GET",
 "headers": {
 "x-rapidapi-key": "key",
 "x-rapidapi-host": "currency-converter5.p.rapidapi.com"
 }
})

.then(response => response.json())
.then(result => {
  console.log(result);
})
.catch(err => {
    console.error(err);
});

This is my response

{
  "base_currency_code": "USD",
  "base_currency_name": "United States dollar",
  "amount": "1.0000",
  "updated_date": "2021-05-07",
  "rates": {
    "TRY": {
      "currency_name": "Turkish lira",
      "rate": "8.2846",
      "rate_for_amount": "8.2846"
    }
  },
  "status": "success"
}

I just want to take rate value. @J. D. @russian-dima @Yisrael (Wix) I need help!

So if you just need the rate-value, why you don’t first get the right result and then transform to JSON?

Sorry, you do already get it as JSON (my bad).

What do you get for?

console.log(result.rates) ???

Try this one for testing…

let results = {
 "base_currency_code": "USD",
 "base_currency_name": "United States dollar",
 "amount": "1.0000",
 "updated_date": "2021-05-07",
 "rates": {
 "TRY": {
 "currency_name": "Turkish lira",
 "rate": "8.2846",
 "rate_for_amount": "8.2846"
 }
 },
 "status": "success"
}

console.log(results)
console.log(results.rates)
console.log(results.rates.TRY)

This API only gives one currency rate. So I write for three rate but sometimes doesn’t show rate when I refresh page 2 or 3 times. Why?


import {fetch} from 'wix-fetch';

fetch("https://currency-converter5.p.rapidapi.com/currency/convert?format=json&from=USD&to=TRY&amount=1", {
 "method": "GET",
 "headers": {
 "x-rapidapi-key": "key",
 "x-rapidapi-host": "currency-converter5.p.rapidapi.com"
 }
})

.then(response => response.json())
.then(result => {
  console.log(result);
  $w("#text54").text=String(result.rates.TRY.rate).slice(0,4);
})
.catch(err => {
    console.error(err);
});

fetch("https://currency-converter5.p.rapidapi.com/currency/convert?format=json&from=EUR&to=TRY&amount=1", {
 "method": "GET",
 "headers": {
 "x-rapidapi-key": "key",
 "x-rapidapi-host": "currency-converter5.p.rapidapi.com"
 }
})
.then(response => response.json())
.then(result => {
  console.log(result);
  $w("#text55").text=String(result.rates.TRY.rate).slice(0,4);
})
.catch(err => {
    console.error(err);
});

fetch("https://currency-converter5.p.rapidapi.com/currency/convert?format=json&from=GBP&to=TRY&amount=1", {
 "method": "GET",
 "headers": {
 "x-rapidapi-key": "key",
 "x-rapidapi-host": "currency-converter5.p.rapidapi.com"
 }
})
.then(response => response.json())
.then(result => {
  console.log(result);
  $w("#text107").text=String(result.rates.TRY.rate).slice(0,5);
})
.catch(err => {
    console.error(err);
});