I'm trying to use sqlite3 in a project and I'm running into a problem. My functions aren't returning any values. I added a console.log before the return statement and oddly enough, it works, but logs after the function has returned.
console.log(getNext());
function establishConnection(callback) {
const db = new sqlite3.Database('database.db');
db.serialize(() => {
callback(db);
});
db.close();
}
function getNext() {
establishConnection(db => {
db.get('SELECT col1, col2 FROM table ORDER BY priority LIMIT 1;', (err, row) => {
console.log([row.col1, row.col2]); // works
return [row.col1, row.col2]; // doesn't work
});
});
}
I'm no expert at Node but this seems like something that should work and I don't understand why it doesn't. Could someone please explain this to me and possibly provide a solution?