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

173
Visualizações
Can't read my variable outside of my function using Cheerio (JavaScript)

I am new to JavaScript and I am having troubles with my web-scraper program. I followed a tutorial to get cyptocurrencies value out of a website, but for some reasons I can't read the variable "price" outside of my function. Here's my code

const axios = require("axios")
const cheerio = require("cheerio")
const{ Routes } = require("discord-api-types/v9");
const { parse } = require("dotenv");

    async function getPriceFeed() {
        try {
            const siteUrl = "https://coinmarketcap.com/currencies/raptoreum/"

            const {data} = await axios({
                method : "GET",
                url: siteUrl,
            })

            const $ = cheerio.load(data)
            const elemSelector = "#__next > div.bywovg-1.fUzJes > div.main-content > div.sc-57oli2-0.comDeo.cmc-body-wrapper > div > div.sc-16r8icm-0.eMxKgr.container > div.n78udj-0.jskEGI > div > div.sc-16r8icm-0.kjciSH.priceSection > div.sc-16r8icm-0.kjciSH.priceTitle > div"

            $(elemSelector).each((parentIdx, parentElem) => {
                    $(parentElem).children().each((childIdx, childElem) =>{
                       var price = $(parentElem).text();
                       return price;
                })
            })
        }catch(err) {
            console.error(err)
        }
    };

console.log(price)

Thank you for your help, Vicriya

EDIT :

I tried to declare my variable outside of my function, my program no longer crashes but the console now logs me undefined. It should log the crypto price instead.

const axios = require("axios")
const cheerio = require("cheerio")
const{ Routes } = require("discord-api-types/v9");
const { parse } = require("dotenv");
var price;
    async function getPriceFeed() {
        try {
            const siteUrl = "https://coinmarketcap.com/currencies/raptoreum/"

            const {data} = await axios({
                method : "GET",
                url: siteUrl,
            })

            const $ = cheerio.load(data)
            const elemSelector = "#__next > div.bywovg-1.fUzJes > div.main-content > div.sc-57oli2-0.comDeo.cmc-body-wrapper > div > div.sc-16r8icm-0.eMxKgr.container > div.n78udj-0.jskEGI > div > div.sc-16r8icm-0.kjciSH.priceSection > div.sc-16r8icm-0.kjciSH.priceTitle > div"

            $(elemSelector).each((parentIdx, parentElem) => {
                    $(parentElem).children().each((childIdx, childElem) =>{
                       price = $(parentElem).text();
                       return price;
                })
            })
        }catch(err) {
            console.error(err)
        }
    };

console.log(price)
about 4 years ago · Santiago Trujillo
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