I have an API running in AWS Lambda written on Node.JS (6.10.3), using the npm package mysql (2.13.0). This is deployed and managed using Serverless.
I am Inserting a single row into a table on a MariaDB RDS instance using the following code:
var mysql = require('mysql');
var connection = mysql.createConnection({
"host": process.env.CONFIG_HOST,
"user": process.env.CONFIG_WRITE_USER,
"password": process.env.CONFIG_WRITE_PW,
"database": process.env.CONFIG_DB,
"connectionLimit": 1
});
module.exports.post = (event, context, callback) => {
var body = JSON.parse(event.body);
var params = event.someParameter;
var sql = getSql(body.name);
console.log(sql);
connection.query(sql, onQueryComplete(params, callback));
}
const onQueryComplete = function(params, callback) {
return function(error, result) {
if (error) {
console.log(error);
callback(null, 'some error response');
}
// do something else...
}
}
function getSql(name) {
return `
INSERT INTO lds_config.test_table
(
name,
name_hash
)
VALUES
(
'${name}',
MD5('${name}')
);`;
}
If I check the table, I can see that the insert has completed successfully and the new row has been added, however error is being set - meaning something has gone wrong somewhere (possibly after the insert).
The error returned by mysql (in console.log) is:
{
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'HANDLER RESOLVED _____');
// Timeout clearing if needed
' at line 2
at Query.Sequence._packetToError (...\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Query.ErrorPacket <<stack trace continues>>)
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
index: 0
}
This is followed by a second error, stating that callback is not a function:
Debug: internal, implementation, error
TypeError: Uncaught error: callback is not a function
Similar code is working elsewhere in the API for selects. Thanks!
Further notes: The table is:
CREATE TABLE lds_config.test_table(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(500), name_hash CHAR(32));
The result from console.log(sql); is:
INSERT INTO lds_config.test_table
(
name,
name_hash
)
VALUES
(
'test01',
MD5('test01')
);
Which works when I run it directly in mysql workbench.