I’m trying to get the custom database connection to work. I was told to try the “Get user” method first to test the database connection. I’ve added the code below but I get an error “Script generated an unhandled asynchronous exception.”
function getByEmail(email, callback) {
console.log(“Executing Get User script…”);
const mysql = require(‘mysql@2.16.0’);
const connection = mysql.createConnection({
host: configuration.DB_HOST,
user: configuration.DB_USER,
password: configuration.DB_PASSWORD,
database: configuration.DB_NAME,
ssl: {
rejectUnauthorized: false,
cert: new Buffer(configuration.DB_CLIENT_CERT, ‘base64’)
}
});
console.log(“Preparing connection to DB…”);
connection.connect();
console.log(“Connected to DB: OK!”);
const query = ‘SELECT id, email FROM users WHERE email = ?’;
console.log("Query DB for email: " + email);
connection.query(query, [ email ], function(err, results) {
console.log("Error???: " + err);
if (err || results.length === 0) return callback(err || null);
console.log(“Query executed OK!”);
console.log("Users found: " + results.length);
if (results.length === 0) return callback();
const user = results[0];
callback(null, {
user_id: user.id.toString(),
email: user.email
});
});
}