0

I have a main.js file and a DBHalnder.js module file. Using sqlite3 I perform a SELECT query inside the module. The query retrieves the correct result inside the module, but I can't export it outside; from main.js file, the variable minPrice is undefined. What am I doing wrong? Furthermore, the IDE warns me that retrieveMinPrice seems to be a void function.

main.js

const  dbHandler  = require('./DBHandler');
var minPrice = dbHandler.retrieveMinPrice("PLAYER");
console.log(minPrice); //it doesn't work --> undefined

DBHandler.js

const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('DB/database.sqlite.db');

module.exports = {
    retrieveMinPrice: function (playerName) {
        db.serialize(function() {
            var sql_query = "SELECT MIN(ethPrice) FROM cards WHERE playerName = ?";
            var values = [playerName];
            db.get(sql_query, values, function (err, rows) {
                console.log(rows['MIN(ethPrice)']); //it works
                return(rows['MIN(ethPrice)']);
            });
        });
    }

}

EDIT after Mushroomator comment:

main.js

const  dbHandler  = require('./DBHandler');
var promise = new Promise(function(resolve, reject) {
                dbHandler.retrieveMinPrice("PLAYER", resolve, reject);
});
promise.then(function(result) {
    console.log(result); // "Stuff worked!"
}, function(err) {
    console.log(err); // Error: "It broke"
});

DBHandler.js

retrieveMinPrice: function (playerName, resolve, reject){
        db.serialize(function() {
            var sql_query = "SELECT MIN(ethPrice) FROM cards WHERE playerName = ?";
            var values = [playerName];
            db.get(sql_query, values, function (err, rows) {
                if (err) {
                    console.log(err);
                    reject(err);
                } else {
                    resolve(rows['MIN(ethPrice)']);
                }

            });
        });
    }
SctALE
  • 509
  • 2
  • 10
  • 30
  • 1
    Does this answer your question? [undefined when returning the query results from sqlite storage in react native](https://stackoverflow.com/questions/48408957/undefined-when-returning-the-query-results-from-sqlite-storage-in-react-native). Your code executes asynchronously hence it will immediately return but nothing is returned so what is returned is `undefined`. The database needs some time to do the processing and only when it is done the callback function is called. When this happens your function has already returned `undefined` and hence the second `return` does nothing. – Mushroomator Apr 23 '22 at 14:33
  • Ok, I understand the problem, but I don't know how to use promises in this case. Should I modify only the code in DBHalnder.js or also the code in main.js? I suppose that I should change both the caller file and the module, but I don't know how – SctALE Apr 23 '22 at 14:51
  • @Mushroomator I edited the question; did I use promises correctly? – SctALE Apr 23 '22 at 14:59
  • Shouldn't db.serialize() handle the asynchronous requests? Each command inside the serialize() function should be guaranteed to finish executing before the next one starts... – SctALE Apr 23 '22 at 15:05

0 Answers0