I'm trying to add mySQL to passport.js to authenticate users in express.js, but can't seem to get await working.
Server.js:
initializePassport(
passport,
function(email) {
pool.getConnection(function(err, connection) {
if (err) throw err;
console.log("Connected!");
pool.query("SELECT * FROM users WHERE email = ?", email, function (err, result) {
if (err) throw err;
return result[0];
connection.release();
});
});
},
)
The passport config
function initialize(passport, getUserByEmail) {
const authenticateUser = async (email, password, done) => {
try {
const user = await getUserByEmail(email);
console.log(user)
} catch (e) {
return done(e)
}
Right now it just prints undefined for user, and then prints Connected. I'm not sure why the await user isn't working.
Well if that's getUserByEmail(), then it doesn't return a promise that is connected to when it's asynchronous operations are done, therefore, doing await getUserByEmail() doesn't wait for anything.
await ONLY does something useful if you are awaiting a promise that is connected to the operation you want to await for. Since you aren't even awaiting a promise, that await does nothing useful. You would need to change getUserByEmail() so that it returns a promise that is connected to the asynchronous operation you're trying to wait for.
For a function to return a promise that is connected to the asynchronous operations, you need to use promise-based asynchronous operations, not plain callback asynchronous operations, everywhere in that function. These are all database operations and all modern databases have a promise-based interface now so what you really want to do is to switch .getConnection(), .query() and .release() to all use promise-based operations. This will also make it a lot simpler to implement proper error handling and proper communication back to the caller of errors.
I don't know mysql particularly well myself, but here's a general idea. The promise interface comes from the module mysql2/promise:
const mysql = require('mysql2/promise');
const pool = mysql.createPool({...});
initializePassport(passport, async function(email) {
let connection;
try {
connection = await pool.getConnection();
console.log("Connected!");
let result = await pool.query("SELECT * FROM users WHERE email = ?", email);
return result[0];
} catch(e) {
// log the error and the rethrow so the caller gets it
console.log(e);
throw e;
} finally {
if (connection) {
connection.release();
}
}
});
Related
I am trying to connect an external (not AWS) MySql server from an AWS Lambda function written in Node.js using nodejs14.x environment, but the connect() callback is not called.
I am been struggling with this problem since days, there are a lot of references to similar issues but I really tried all possible permutations of solutions I found.
I am deploying with SAM and testing both on local machine and on real AWS.
Here is the sample code of the lambda helper
const mysql = require('mysql');
exports.helloFromLambdaHandler = async () => {
const message = 'Hello from Lambda!';
console.info(`${message}`);
var sql = "SELECT 1+? AS sum";
var values = [1];
console.log("Doing createConnection");
const connection = mysql.createConnection({
/* my connection data */
});
console.log("Doing connect");
connection.connect( (err) => {
console.log("Inside connection callback");
console.log('connected as id ' + connection.threadId);
if(!err) {
console.log("DB connected, thread id is " + connection.threadId);
console.log("Doing query");
connection.query(sql, values, (err, result, values) => {
console.log("Inside query callback");
if(!err) {
console.log("Query ok!");
console.log(result);
connection.end();
} else {
console.log("Error executing query: " + err.message);
}
});
} else {
console.log("Error connecting db: "+ err.message);
}
});
console.log ("Returning...");
return message;
}
The log is
Hello from Lambda!
Doing createConnection
Doing connect
Returning...
The expected behaviour is that after "Returning..." I should see the log "Inside connection callback" then "Inside query callback" and then "Query ok!".
Instead the callback of connect() appears not invoked.
I know that I can call query() directly skipping connect() but also doing so I encounter same issue.
Any clue?
Thank you!
SOLUTION
As suggested by the accepted answer, returning a promise is the solution to let Node complete all the queue. Unfortunately it's not possible to complete the Lambda and leave it running in background in a safe manner, for what I understand.
I am investigating alternative solutions such as:
mysql2 library which supports promises natively
serverless-mysql npm package which handles shared db connections
Below the running demo code
const mysql = require('mysql');
exports.helloFromLambdaHandler = async (event, context) => {
const message = 'Hello from Lambda!';
console.info(`${message}`);
var sql = "SELECT 1+? AS sum";
var values = [1];
console.log("Doing createConnection");
const connection = mysql.createConnection({
/* my connection data */
});
console.log("Doing query");
const promise = new Promise( (resolve, reject) => {
connection.query(sql, values, (err, result, values) => {
console.log("Inside query callback");
if(!err) {
console.log("Query ok!");
console.log(result);
connection.end();
resolve(message);
} else {
console.log("Error executing query: " + err.message);
reject(err);
}
});
});
console.log ("Returning...");
return promise;
}
You are using async handler, thus your function probably completes before your connect() has a chance to execute.
To try to overcome the issue, you can use Promise as shown in AWS docs.
I'm using node.js and express, also mysql.
I use a connection pool to request connections and create a promise on it, to limit callback nightmare, the following snippet is set in a file that I import later, note that that I set an handler on error to not terminate the application in case of anything going really wrong
exports.getConnection = () => {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
if (err) {
reject(`Could not obtain the connection from the pool: ${err}`);
}
connection.on('error', err => {
console.log(`SQL error (code: ${err.code}, message: ${err.sqlMessage}) while executing query: ${err.sql}`);
});
resolve(connection);
});
});
};
And here is an example of usecase (the idea is to get the connection, chain the query in the then, and if a non fatal error happen I will throw it and handle the connection release in the catch handler
// Exception handler that release the connection then call the callback
function releaseConnectionHandler(err, connection, callback) {
connection.release();
callback(err, null);
}
exports.someRequest = function(ID, callback) {
sqlPool.getConnection().then(connection => {
connection.query("SELECT * from tableNotExists",
(err, result) => {
if (err) {
throw ({ err, connection, callback });
}
connection.release();
callback(null, result);
});
}).catch(({ err, connection, callback}) => releaseConnectionHandler(err, connection, callback));
};
The query will fail, but I see that the handler is not even called (I put some trace in it...) and the application terminates on
node_modules/mysql/lib/protocol/Parser.js:80
throw err; // Rethrow non-MySQL errors
Correct querie yoeld no troubles...Any ideas what I did wrong on the error handling ?
You're re-throwing the error passed to the callback of your query, which the library you're using then re-throws as well, and finally isn't properly caught and handled anywhere and results in a failure. You're not in the context of the Promise when you throw, but the context of the callback function called from the mysql module.
You're also unnecessarily mixing promises and callbacks, in particular the function you're exporting. Your question indicates that you want to move away from callbacks, so I'm going to base this answer on that indication.
To solve the main issue, don't throw the error. Instead, pass it up to the callee:
const promisify = require("util").promisify;
exports.someRequest = function (ID) {
return sqlPool.getConnection().then(connection => {
return promisify(connection.query)("select * from tableNotExist")
.finally(connection.release);
});
};
The connection will always be released back to the pool, whether successful or on error. You can then call the method with:
yourModule.someRequest(id).then((results) => {
// Do something with the result set
}).catch((e) => {
// Handle error. Can be either a pool connection error or a query error.
});
If you have the possibility to use async/await, the code can be rewritten:
const promisify = require("util").promisify;
exports.someRequest = async function (ID) {
let connection = await sqlPool.getConnection();
try {
return await promisify(connection.query)("select * from tableNotExist");
} finally {
connection.release();
}
};
I also recommend using node-mysql2 since they have a Promise-based API in addition to their callback-style API and in my experience better performance as well. Then you don't have to write these tedious wrappers and instead just require('mysql2/promise') and be good to go.
I'm trying to retrieve data from a database and send it back to the user, but since mysql queries work asynchronously, I can't just put the code that sends the response after the query code, I have to send the response within the callback function of the query.
var express = require('express');
var fs = require('fs');
var mysql = require('mysql')
var app = express();
var con = mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"tempdb"
})
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT,
PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-
type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.get('/',function(req,res,next){
function foo(res){
con.query('SELECT * FROM `data` WHERE 1',function(err,result){
if (err) throw err;
res.end('weee',function(err){
if (err) throw err;
)}
})
}
foo(res)
next();
})
app.listen(1001)
Even with something as simple as this, when the data from the database isn't processed or used at all, the res.end function doesn't send any data.
I've tried many variations of this, using waterfall callbacks, putting the res.end in a function outside of the query and calling it, but nothing seems to work. The only time it works it when I put it outside the query, but since the query is async I can't get any data out of it that way. Is there a way to work around this or am I just missing something?
Thanks in advance!
Remove the call to next() in your app.get() handler.
You only want to do that when you are NOT handling the request and sending a response and you want to let other handlers have a crack at the request.
The way you have it, you are calling next() BEFORE the con.query() finishes so that lets the default handler in express handle the request and thus your res.end() doesn't actually do anything because a response has already been sent for this request.
Here's what I would suggest:
app.get('/', function(req, res){
con.query('SELECT * FROM `data` WHERE 1',function(err, result){
if (err) {
console.log(err);
res.status(500).end();
} else {
res.send('weee');
}
});
});
So, you only call next() if you are not going to be sending a response and you want someone else in the handler chain to send the response.
When using res in middlewares throw a return before res.
app.get('/',function(req,res,next){
function foo(res){
con.query('SELECT * FROM `data` WHERE 1',function(err,result){
if (err) throw err;
return res.end('weee',function(err){
if (err) throw err;
)}
})
}
foo(res)
next();
})
Hopefully this works...
I am getting strange behavior using Node.JS and MySQL with this driver - https://github.com/mysqljs/mysql
Essentially, I have a button on the frontend that triggers an app.get that makes a query in the database and I can happily use the results in my backend.
This works nicely, until I press the button 4-5 times in a second, where as the queries lock up and I have to wait for 2-3 minutes until they continue executing. I have a similar write function that behaves the same way.
Is it possible this is a problem, because I'm trying to execute the exact same query asynchronously? I.e. do I have to limit this from the front end or is it a backend problem?
Any ideas on how to debug what exactly is going on?
// database.js
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 100,
host : 'localhost',
user : 'secret',
password : 'secret',
database : 'mydb'
});
exports.getConnection = function(callback) {
pool.getConnection(function(err, connection) {
callback(err, connection);
});
};
// dbrw.js
var con = require('../config/database');
function read(id, done) {
con.getConnection(function(err, connection){
if(!err){
connection.query("SELECT * FROM users WHERE id = ?",[id], function(err, rows) {
connection.release();
if (err)
done(err);
if (rows.length) {
console.log("rows " + JSON.stringify(rows));
done(rows[0].progress);
};
});
}
else {
console.log(err);
}
});
}
exports.read = read;
// routes.js
var dbrw = require('./dbrw.js');
app.get('/read', isLoggedIn, function(req, res) {
dbrw.read(req.user.id, function(result) {
console.log(result);
});
});
// Frontend - angular app.js
$scope.tryread = function() {
$http.get('/read');
}
Thanks in advance for any input.
I see a few issues:
function read(id, done) {
con.getConnection(function(id, connection){...}
}
Notice how you overwrite the id passed to read by giving that same name to an argument of the callback to getConnection.
Also, your Express route doesn't actually end the request by sending back a response, which will make your browser time out the connection. At some point, it will even refuse to send more requests because too many are still pending.
So make sure to end the request:
app.get('/read', isLoggedIn, function(req, res) {
dbrw.read(req.user.id, function(result) {
console.log(result);
res.end(); // or `res.send(result)`
});
});
And a tip: you should use the callback calling convertion for Node, where the first argument represents an error (if there is any) and the second argument represents the return value.
I have the following function that gets a hexcode from the database
function getColour(username, roomCount)
{
connection.query('SELECT hexcode FROM colours WHERE precedence = ?', [roomCount], function(err, result)
{
if (err) throw err;
return result[0].hexcode;
});
}
My problem is that I am returning the result in the callback function but the getColour function doesn't return anything. I want the getColour function to return the value of result[0].hexcode.
At the moment when I called getColour it doesn't return anything
I've tried doing something like
function getColour(username, roomCount)
{
var colour = '';
connection.query('SELECT hexcode FROM colours WHERE precedence = ?', [roomCount], function(err, result)
{
if (err) throw err;
colour = result[0].hexcode;
});
return colour;
}
but of course the SELECT query has finished by the time return the value in colour
You have to do the processing on the results from the db query on a callback only. Just like.
function getColour(username, roomCount, callback)
{
connection.query('SELECT hexcode FROM colours WHERE precedence = ?', [roomCount], function(err, result)
{
if (err)
callback(err,null);
else
callback(null,result[0].hexcode);
});
}
//call Fn for db query with callback
getColour("yourname",4, function(err,data){
if (err) {
// error handling code goes here
console.log("ERROR : ",err);
} else {
// code to execute on data retrieval
console.log("result from db is : ",data);
}
});
If you want to use promises to avoid the so-called "callback hell" there are various approaches.
Here's an example using native promises and the standard MySQL package.
const mysql = require("mysql");
//left here for testing purposes, although there is only one colour in DB
const connection = mysql.createConnection({
host: "remotemysql.com",
user: "aKlLAqAfXH",
password: "PZKuFVGRQD",
database: "aKlLAqAfXH"
});
(async () => {
connection.connect();
const result = await getColour("username", 2);
console.log(result);
connection.end();
})();
function getColour(username, roomCount) {
return new Promise((resolve, reject) => {
connection.query(
"SELECT hexcode FROM colours WHERE precedence = ?",
[roomCount],
(err, result) => {
return err ? reject(err) : resolve(result[0].hexcode);
}
);
});
}
In async functions, you are able to use the await expression which will pause the function execution until a Promise is resolved or rejected. This way the getColour function will return a promise with the MySQL query which will pause the main function execution until the result is returned or a query error is thrown.
A similar but maybe more flexible approach might be using a promise wrapper package of the MySQL library or even a promise-based ORM.