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!

2
  • You should do reject(err) in your catch block as well. Commented Mar 5, 2019 at 6: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 Commented Mar 5, 2019 at 6:30

2 Answers 2

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); }); 
Sign up to request clarification or add additional context in comments.

9 Comments

I removed the 'return' before reject() and resolve() as you suggested. Still not working =/
I have removed that answer, and added a new answer. pls check it
Well, I did it exactly as you said, sir: var cod_customer = 1; var isexpired = isExpired(cod_customer); console.log(isexpired);//returns 'indefined' =(
I have updated my answer again you should pass the promise again and use the then method when you call the isExpired
Ok, again, did as you told me to, sir. Now the console.log says "Promise { <pending> }"
|
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; }); };

4 Comments

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.
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?
already explained in answer that you cant directly get the response from promise, you will have to use .then or async await
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.