I'm new to NodeJS, and JS in general (mostly a PHP and C# guy), so I could really use some help with this function below.
The goal is to receive a JSON payload, connect to MySQL, and then return the results of the query in a JSON response. I've got it connecting to the DB, I can read the JSON data that it receives (event.fieldname) but for some reason it's not sending back the JSON for the applicant_data variable.
Do I just have the variable in the wrong location? When I run the code below I just get back "{}" as the returned data.
Thanks in advance for the help!
NodeJS Code:
exports.handler = function(event, context, callback) {
console.log('Starting:');
console.log("Request received:\n", JSON.stringify(event));
var mysql = require('mysql');
var jsonconnection = mysql.createConnection({
host: 'servername',
user: 'username',
password: 'password',
database: 'database'
});
jsonconnection.connect();
console.log('Connected to MySQL:');
jsonconnection.query('SELECT applicant_id FROM customers WHERE applicant_id = \'' + event.applicant_id + '\'',
function(err,res){
if(err) throw err;
console.log('Row Details:', JSON.stringify(res));
var applicant_data = {
applicant_id : res.applicant_id
};
jsonconnection.end();
context.succeed(applicant_data);
})
};
console.log('Row Details:', JSON.stringify(res));console.log('Row Details:')Row Details: [{"applicant_id":"WQFV7CWXA39E9TXT"}]