Reputation: 646
So I thought I was doing this right, but I guess not. I am trying to pass the results from the query to the function in then()
. I have a console.log()
logging results from inside the two functions. The first one spits out the results as it should. The second one is giving me undefined
and I can't figure out what I'm doing wrong.
var dbConnect = () => new Promise(
(res, rej) => {
var connection = mysql.createPool(Config.mariaDBCred);
connection.getConnection((err, connection) => {
if(err) return rej(err);
return res(connection);
});
}
);
var dbQuery = (connection, queryString, paramArray) => new Promise(
(res, rej) => {
var sql = mysql.format(queryString, paramArray);
connection.query(sql,
(err, results, fields) => {
connection.release();
console.log(results); //THIS DISPLAYS RESULTS FROM THE QUERY CORRECTLY
if(err) return rej(err);
return res(results, fields);
}
);
}
);
//CHECK IF EMAIL EXISTS
module.exports.doesEmailExist = (email, callback) => {
dbConnect().then(
(connection) => {
dbQuery(
connection,
'SELECT `id`, `password_hash` FROM `users` WHERE email = ?',
[email]
)
}
).then(
(results, fields) => {
console.log(results); //THIS DISPLAY UNDEFINED
if(results.length > 0) return callback(true, results);
return callback(false, "Email does not exist.");
}
).catch(
(reason) => {
console.log(reason);
return callback(false, "Internal Error");
}
);
}
Upvotes: 1
Views: 969
Reputation: 2214
I'm not sure if the edits below will resolve all of your issues, but in terms of your problems with regards to the Promise
API, they should steer you in the right direction.
First, in the dbQuery
function, I resolve an object because you can only resolve a single value in a promise.
Second, to chain promises, you must return promises. In your first then
handler you weren't returning the Promise
from dbQuery
(after dbConnect
).
Finally, I changed your second then
handler to work with the single resolved object, rather than the multiple parameters used previously. Before, had everything worked, results
would have been defined, but not fields
. It's good practice to resolve an Array
or an Object
in cases like these. If your using es6, object/array destructuring makes ease of this.
Another note. If you are using Promises, consider ditching the callback pattern implemented in your doesEmailExist
function, if not altogether. It's more consistent, and you won't have to wrap catch
handlers, unless targeting specific error cases. Food for thought.
var dbConnect = () => new Promise(
(res, rej) => {
var connection = mysql.createPool(Config.mariaDBCred);
connection.getConnection((err, connection) => {
if(err) return rej(err);
return res(connection);
});
}
);
var dbQuery = (connection, queryString, paramArray) => new Promise(
(res, rej) => {
var sql = mysql.format(queryString, paramArray);
connection.query(sql,
(err, results, fields) => {
connection.release();
console.log(results); //THIS DISPLAYS RESULTS FROM THE QUERY CORRECTLY
if(err) return rej(err);
// return res(results, fields); NOPE, can only resolve one argu
return res({ results: results, fields: fields }) // resolve an object
}
);
}
);
//CHECK IF EMAIL EXISTS
module.exports.doesEmailExist = (email, callback) => {
dbConnect().then(
(connection) => {
// Return the promise from `dbQuery` call
return dbQuery(
connection,
'SELECT `id`, `password_hash` FROM `users` WHERE email = ?',
[email]
)
}
).then(
(response) => {
// Can only resolve one argument
var results = response.results;
var fields = response.fields;
console.log(results); //THIS DISPLAY UNDEFINED
if(results.length > 0) return callback(true, results);
return callback(false, "Email does not exist.");
}
).catch(
(reason) => {
console.log(reason);
return callback(false, "Internal Error");
}
);
}
Upvotes: 1