0

I'm new to JS and I don't understand a bit why my code doesn't work


let allposts = [];
//open db
let db = new sqlite3.Database('./data.db', sqlite3.OPEN_READWRITE, (err) => {
    if (err) {
        console.error(err.message);
    }
    console.log('Connected to the database.');
    });
    //main request
db.all('SELECT * FROM main', (err, rows) => {
        if (err) {
          console.error(err.message);
        }
        rows.forEach(row =>{
            allposts.push('<div class="post"><h3>'+row.postname+'</h3><p class="text">'+row.posttext+'</p></div>');
        });
    });
    // closing db
db.close( (err) => {
    if (err) {
        return console.error(err.message);
    }
    console.log('Close the database connection.');
});
console.log(allposts);

//Output
>>node db.js
[]
Connected to the database.
Close the database connection.

I'm trying to pull data from the database and by sorting and adding them to templates, transfer them to array. That's all I got:

Sanorian
  • 1
  • 1

0 Answers0