2
My Vue.js function for currency conversion using the API of http://free.currencyconverterapi.com returns Nan, instead of returning the currency conversion.
<template>
<div class="conversor">
<h2>{{moedaA}} Para {{moedaB}}</h2>
<input type="text" v-model="moedaA_value" v-bind:placeholder="moedaA">
<input type="button" value="Converter" v-on:click="converter">
<h2>{{moedaB_value}}</h2>
</div>
</template>
<script>
export default {
name: "Conversor",
props: ["moedaA","moedaB"],
data(){
return {
moedaA_value :"",
moedaB_value : 0
};
},
methods:{
converter(){
let de_para = this.moedaA + "_" + this.moedaB;
let url = "http://free.currencyconverterapi.com/api/v5/convert?q="+
de_para
+"&compact=ultra&apiKey=xxxxxxxxxxxxxxxxxxxx";
fetch(url)
.then(res=>{
return res.json()
})
.then(json =>{
let cotacao = json[de_para].val;
this.moedaB_value = (cotacao * parseFloat(this.moedaA_value)).toFixed(
2
)
})
}
}
};
</script>
<!-- O scoped serve para limitar o estilo apenas ao arquivo em que a tag é usada-->
<style scoped>
</style>
What gives
console.log(url)
? Is this your/real API key? this is a public space... you should not put private data here.– Sergio
Thanks for the touch of Key, had not attacked me, console.log(url) brings me the correct url, when using the url in the browser it brings me the following json { USD_BRL: 3.939041 }
– Victor Pacheco Mendonça Soares