Considering these two files: main.js
const dbHandler = require('./DBHandler');
var minPrice = dbHandler.retrieveMinPrice("PLAYER");
console.log(minPrice);
DBHandler.js
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('DB/database.sqlite.db');
module.exports = {
retrieveMinPrice: function (playerName){
var to_be_returned= 9999;
db.serialize(function() {
var sql_query = "SELECT MIN(ethPrice) FROM cards WHERE playerName = ?";
var values = [playerName];
db.get(sql_query, values, function (err, rows) {
to_be_returned = rows['MIN(ethPrice)'];
console.log(to_be_returned); //right value 1234 (example)
});
console.log(to_be_returned); //wrong value 9999
});
console.log(to_be_returned); //wrong value 9999
}
}
What is the right way to return the variable to_be_returned to the main.js file, in order to have the minPrice variable set to 1234 and not 9999 (or undefined)? Should I use a different function than db.get()? Is db.serialize() used in the wrong way?