Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!