Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Display value of a response from an api at JavaScript

I'm trying to create a calculator in JavaScript. In this calculator I have a button to convert the number inputed by user to dollars. I have an api with a key but somehow I can't make it to display the result on the calculator.

case 'convert':
            let eur = parseFloat(num)
            console.log(eur)
            const url = 'https://v6.exchangerate-api.com/v6/my-api-key/pair/EUR/USD/' + eur
            fetch(url)
                .then(response => response.json())
            let converted = JSON.parse(response.json())
            display.value = converted
            num = 0
            break
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Method 1:

You can do asyn call your function with await the APIs request

let response = await fetch(url);
let converted = JSON.parse(response.json())
display.value = converted

Method 2:

Move your parsing code inside the APIs response, and get the converted values and set it to display field

about 4 years ago · Juan Pablo Isaza Relatório

0

The first problem you have is that you're not processing the API result properly.

response only exists inside your Promise callback. In the initialization of your converted variable, response is not defined. Following should at least successfully give you the data from the API which you can then use for display. Besides, calling .json() on a fetch Response already converts the HTTP body to JSON so there is no need to parse it again with JSON.parse().

fetch('https://v6.exchangerate-api.com/v6/my-api-key/pair/EUR/USD/4')
    .then(response => response.json())
    .then((responseData) => {
        console.log(responseData);
        display.value = responseData;
    });

As you haven't provided information on what is behind display.value, you may encounter further issues. Also, I hope you have recognized that an API key for this API is required and your URL just says 'my-api-key'.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda