Reputation: 173
I'm currently working with chatbots and trying to work out the new await / async structure in NodeJs.
My code currently looks like this:
exports.checkUser = async function (userId, callback){
let result;
try{
result = await pool.query('SELECT * FROM users WHERE id = ?;',
[userId]);
if(!result || result.length == 0)
{
return false;
}
else if(result.length != 0)
{
return true;
}
}
catch(err)
{
console.log(err);
callback(err, null);
}
};
I went under the assumption I don't have to fiddle with promises while using asyc/await structure. I am calling this function somewhere else like this:
mysql.checkUser(user).then((result) =>
{
//console for debug
console.log(result);
});
Well, I know the checks for the success are pretty plain, I don't seem to get a decent result.
It's always throwing me something like inside the result object:
_callSite: Error
at Pool.query (/app/node_modules/mysql/lib/Pool.js:199:23)
at Object.exports.checkUser (/app/lib/mysql/mysql.js:49:29)
at Object.exports.checkUser (/app/lib/auth/authentication_engine.js:28:11) [...]
Upvotes: 0
Views: 2459
Reputation: 365
pool.query is not returning a Promise, so the execution will fail when it is called as an async function. It is indeed a good idea to use mysql2 package, more on: https://www.npmjs.com/package/mysql2#using-promise-wrapper
Upvotes: 4