I am writing some database middleware for some expressjs app that is based on sqlite3. This is my directory structure:
| app
| database
index.js
| routes
index.js
database/index.js contains:
module.exports.getLinkById = function(id) {
Database.get(`SELECT redir FROM root WHERE id = "${id}"`, [], function(err, rows) {
try {
var row = rows;
console.log(row.redir); // successfully logs the redir attr with correct value
return row;
} catch (e) {
throw e;
}
});
};
and routes/index.js contains:
var database = require('../database');
console.log(database.getLinkById("afb8")); // undefined
In the database/index.js file, the row based on the id is selected and returned. For debugging, I console.log()
ged it and that logs the correct value.
However, even thought I returned the row
value, routes/index.js treats it as undefined.
How do I correctly return
the value so I can access it in routes/index.js
?