Typeerror: Cannot read Property 'Brl' of Undefined [Discordjs]

Asked

Viewed 50 times

0

I’m trying to do a function for my Discord bot that does a request on a cryptocurrency api, it’s working normally if I put the cryptocurrency name manually in the api, but I would like to use an args.Join to make the request be done through the Discord itself, but it gives the following error: Typeerror: Cannot read Property 'Brl' of Undefined.

const Discord = require('discord.js')
const axios = require('axios')
const client = new Discord.Client()


module.exports = class extends Command {
    constructor(...args){
        super(...args, {
            aliases: ['eth']

        })
        
    }

    async run(message, args) {

        let cripto = args.join(" ").toLowerCase();
        let getCotacaoBTC = async () => {
            let response = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cripto}&vs_currencies=brl,usd&include_24hr_change=true`)
        
        Cotacao = response.data;
        return console.log(Cotacao)
    }   

        console.log("Criptomoeda: " + cripto)
        let Cotacao;
        let cotacaoBTC = await getCotacaoBTC();
        let brl = Cotacao.cripto.brl;
        let usd = Cotacao.cripto.usd;
        let variation = Cotacao.ethereum.brl_24h_change;
        let formatado = brl.toLocaleString('pt-br', {style: 'currency', currency: 'BRL'})
        let formatadoUSD = usd.toLocaleString('en-us', { style: 'currency', currency: 'USD'})
        function criptoF() {


        if(variation > 0) {
            let embed1 = new Discord.MessageEmbed()
                .setTitle(`${cripto}`.toUpperCase())
                .setColor("GRAY")
                .addFields(
                    { name: `Cotação Atual BRL`, value: `:flag_br: ${formatadoUSD}`, inline: true},
                    { name: `Cotação Atual USD`, value: `:flag_us: ${formatadoUSD}`},
                    { name: `Variação`, value: `:chart_with_upwards_trend: +${variation.toFixed(2)}%`}
                )
                .setThumbnail(`https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Ethereum_logo_2014.svg/628px-Ethereum_logo_2014.svg.png`)
            message.channel.send(embed1)
        }else if(variation < 0){
            let embed = new Discord.MessageEmbed()
                .setTitle(`${cripto}`.toUpperCase())
                .setColor("GRAY")
                .addFields(
                    { name: `Cotação Atual BRL`, value: `:flag_br: ${formatado}`, inline: true},
                    { name: `Cotação Atual USD`, value: `:flag_us: ${formatadoUSD}`},
                    { name: `Variação`, value: `:chart_with_downwards_trend: ${variation.toFixed(2)}%`}
                )
                .setThumbnail(`https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Ethereum_logo_2014.svg/628px-Ethereum_logo_2014.svg.png`)
                channel1.send(embed)
            }
        }
        criptoF()
        

    }
  • Just below console.log("Criptomoeda") you declare a variable Cotacao and she’s null when she arrives at the let brl = Cotacao.cripto.brl will give error because Quotation has no value

  • I just made these code changes and still gives the same error, what I’m finding strange is that I’m getting the information on the console but at the time of requistar Brl or anything within the Undefined API.

  • to help you profile, you can spread some console.log(any variable), the Node js will print a "tree", showing the properties of the object you do this, this will help you in addition find out if this "quotation.cripto.Brl" really exists for example, discover all.cripto quotation properties, with a simple console.log(quotation.cripto)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.