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

209
Visualizações
Function returns empty arrays but when they are logged to the console the data is shown

I am currently using SQLite3 for my database and interacting with it through NodeJS. I am creating a function that returns two datasets from the table. When I use the db.all function and loop through each row, it seems to access the data just fine. When I attempt to return these datasets in an array, only an empty array of empty datasets is returned.

const sqlite3 = require('sqlite3').verbose();

let db = new sqlite3.Database('./db/ecodata.db', (err) => {
    if (err) {
      console.error(err.message);
    }
    console.log('Connected to the ecosystem database.');
});

// MC
async function EcoMcData(id) {
    let sql = `SELECT * FROM ecosystem_stats where id = ?`;
    let mcData = [];
    let date = [];
    db.all(sql, [id], (err, rows) => {
        if (err) {
            throw err;
            }
            rows.forEach((row) => {
                mcData.push(row.market_cap)
                date.push(row.time);
            });
    });
    return [date, mcData];
}

async function print() {
    let ecod = await EcoMcData("smart-contract-ecosystem");
    console.log(ecod);
}

print();

This is what is returned when I run the above code:

[ [], [] ] Connected to the ecosystem database.

However if I was to console log the row data in the row.forEach loop, it is correctly shown in the console.

Is this an asynchronous problem? Are there any solutions to this?

about 4 years ago · Juan Pablo Isaza
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