1

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!

Aria
  • 3,724
  • 1
  • 20
  • 51
  • You should do `reject(err)` in your catch block as well. – jayarjo Mar 05 '19 at 06:30
  • 1
    you are not returning the promise in selectOneField function it must be return returnResultset(...) and also you cant simply do rtf = db_.selectOneField('view_expiredusers', 'cod', 'cod', cod); .you will have to use async await or then – AZ_ Mar 05 '19 at 06:30

2 Answers2

2

You should return the promise and chain it inside isExpired function.

//wrapper function for testing purposes
var selectOneField = exports.selectOneField = function (tbl, field, pk, pkval) {

var qry_ = "SELECT  " + field + " FROM " + tbl + " WHERE " + pk + " = '" + pkval + "'";

return returnResultset(qry_);

};

//...and on another page I want to be able to receive the results from the function above:

var isExpired = exports.isExpired = function (cod) {

return db_.selectOneField('view_expiredusers', 'cod', 'cod', cod)



};

When you call the isExpired in other files you should use the then method of the promise and return the results. do it as follows

var cod_customer = 1;
var isexpired;
 isExpired(cod_customer).then(function (results) {
        isexpired = results;
        console.log(isexpired);
    }, function (error) {
        console.log("Error: " + error);
    });
Nithya Rajan
  • 4,722
  • 19
  • 30
  • I removed the 'return' before reject() and resolve() as you suggested. Still not working =/ – Fabricio Ganzert Mar 05 '19 at 06:53
  • I have removed that answer, and added a new answer. pls check it – Nithya Rajan Mar 05 '19 at 06:54
  • Well, I did it exactly as you said, sir: var cod_customer = 1; var isexpired = isExpired(cod_customer); console.log(isexpired);//returns 'indefined' =( – Fabricio Ganzert Mar 05 '19 at 06:59
  • I have updated my answer again you should pass the promise again and use the then method when you call the isExpired – Nithya Rajan Mar 05 '19 at 07:01
  • Ok, again, did as you told me to, sir. Now the console.log says "Promise { }" – Fabricio Ganzert Mar 05 '19 at 07:06
  • isexpired is a promise so console.log(isexpired) will print a complete unresolved promise object. – AZ_ Mar 05 '19 at 07:07
  • when you use promises you should console.log() inside the then method, i updated it again. bcz javascript is asynchronous – Nithya Rajan Mar 05 '19 at 07:07
  • @BearNithi javascript is asyncronous? https://stackoverflow.com/questions/16523870/is-javascript-synchronousblocking-or-asynchronousnonblocking-by-default – AZ_ Mar 05 '19 at 07:10
  • Mr Bear, that did the trick. Got the result as it was supposed to be. At first its so confusing how do deal with promises because I'm used to deal with sync functions. New grounds, a lot to learn ahead. You are a life saver, mr Bear! – Fabricio Ganzert Mar 05 '19 at 07:12
1

you are not returning the promise in selectOneField function it must return the promise and also you cant simply do

rtf = db_.selectOneField('view_expiredusers', 'cod', 'cod', cod);

.you will have to use async-await or then

Must be handled this way

//wrapper function for testing purposes
var selectOneField = exports.selectOneField = function (tbl, field, pk, pkval) {

var qry_ = "SELECT  " + field + " FROM " + tbl + " WHERE " + pk + " = '" + pkval + "'";

return returnResultset(qry_).then(function (results) {
    return results;
}).catch(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).then(rtf => {
console.log(rtf);

return rtf;
});


};
AZ_
  • 3,094
  • 1
  • 9
  • 19
  • Ok, so I removed the 'return' before reject() and resolve() as suggested mr Bear Nithi. I also did the changes you suggested mr AZ_. Here's what I got so far: var cod_customer = 1; var isexpired = isExpired(cod_customer); console.log(isexpired); The above returns 'undefined'. Its worth to mention that the query is valid, and all functions but isExpired() are returning the result properly. – Fabricio Ganzert Mar 05 '19 at 06:51
  • adding and return with resolve and reject is a good practice so don't remove that, can you please update the question with new issue? – AZ_ Mar 05 '19 at 07:03
  • already explained in answer that you cant directly get the response from promise, you will have to use .then or async await – AZ_ Mar 05 '19 at 07:05
  • Yes, you got it right mr AZ_. However, I had to actually 'see' the right code in order to understand what I was doing wrong. Anyway, I upvoted your answer as well. – Fabricio Ganzert Mar 05 '19 at 07:58