Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
Get value from SELECT query inside a module - Javascript sqlite3

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)']);
                }

            });
        });
    }
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!