My goal is to wrap MySQL queries, pass the parameters to a function and another function does the MySQL job, returning the results.
Here's my code so far:
//mysql lib var mysql = require('mysql'); //database credentials exports.pool = mysql.createPool({ connectionLimit: 50, host: 'localhost', user: 'root', password: 'password', database: '_app', debug: false }); //my wrapper =( var returnResultset = exports.returnResultset = function (qry) { return new Promise(function (resolve, reject) { try { mysql_.pool.getConnection(function (err, connection) { if (err) { console.log("Error on function returnResultset - MYSQL ERROR: " + err); return reject(err); } connection.query(qry, [], function (error, results, fields) { connection.release(); if (error) { console.log("Error on function returnResultset - MYSQL ERROR: " + error); return reject(error); } return resolve(results); }); }); } catch (e) { console.log('error:' + e); } }); }; //wrapper function for testing purposes var selectOneField = exports.selectOneField = function (tbl, field, pk, pkval) { var qry_ = "SELECT " + field + " FROM " + tbl + " WHERE " + pk + " = '" + pkval + "'"; returnResultset(qry_).then(function (results) { return results; }, function (error) { console.log("Error: " + error); }) }; //...and on another page I want to be able to receive the results from the function above: var isExpired = exports.isExpired = function (cod) { var rtf = db_.selectOneField('view_expiredusers', 'cod', 'cod', cod); console.log(rtf); return rtf; }; The code above returns undefined. I can't get to make this function working properly.
I have tried console.log(results). The query works like a charm. Only thing I can't get to work is to catch the result from an external function.
Any thoughts? Thanks in advance!
reject(err)in your catch block as well.