I am trying to fetch the exchange rate of one currency so that I can use it in my conversion.
currenycodes = new Array("USD", "EUR", "INR", "JPY", "RUB", "GBP", "CAD", "MXN", "CNY", "KRW", "BTC", "ETH");
This is the codes array from which I find the code which the user wants as the base (the user enters the code and I find its index in the array).
var sourcecode = currenycodes[sourceIndex];
var targetcode = currenycodes[targetIndex];
After I find the codes the user wants to exchange between (supposed USD to EUR). I feed these codes to the API.
var url = `https://v6.exchangerate-api.com/v6/c698605f2b9ef5f1ef716047/latest/${sourcecode}`;
async function currency() {
let res = await fetch(url);
let data = await res.json();
let val=data.conversion_rates.targetcode;
console.log(val)
// console.log(data.conversion_rates.EUR);
}
Here conversion* rate is an array of key:value pairs e.g.: {USD: 1, AED: 3.6725, AFN: 85.8224, ALL: 97.9119*}
I need that value for the further conversion process, but I am getting undefined for some reason where the good data.conversion_rates.EUR
gives perfect value.
Any help would be greatly appreciated, I can't do this alone.