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

188
Visualizações
Getting data from json file JavaScript

I have just started working with JavaScript and do not understand much about it. I need help with outputting a variable for a promise, but when I output it it gets undefined. I need to put this variable in the message for the telegram bot, when I put it there, I get: [object Promise]

import fetch from 'node-fetch';

const dataTransfer = fetch('https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json')
  .then(currencyEx => currencyEx.json())
  .then(data => data[0].exchangedate);

console.log(dataTransfer.data);

I get the answer: undefined

function start() {

    function curData(chatId) {
        return bot.sendMessage(chatId, `Current data: ${dataTransfer.data}`);
    }

    bot.on('message', msg => {
        const text = msg.text;
        const chatId = msg.chat.id;

        if (text === '/data') {
            return curData(chatId);
        }

        return bot.sendMessage(chatId, 'idk');
    });
}

This is the telegram bot code, the response to the message / data = Current data: [object Promise]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Fetch returns a promise so const dataTransfer = fetch('') returns a promise, unless you await for it.

What will work is:

import fetch from 'node-fetch';
    
const fetchData = async () => {
  const resp = await fetch('https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json')
  const dataTransfer = await resp.json();

  console.log(dataTransfer);
}

fetchData()
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