0

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.

1 Answer 1

1

Include the query also since the first error is regarding MySQL syntax. About callback is not a function it is not passed as a parameter in below code as far as I see.

const onQueryComplete = function(params) {
  return function(error, result, callback) {
    if (error) {
     console.log(error);
     callback(null, 'some error response');
    }
    // do something else...
  }
}

Above is the corrected one.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - I missed that when copying the code across! I do have the callback being passed to the onQueryComplete function, however it is in the '=function(params, ...)' part. I have amended my original query.
I have now edited my original post to add the sql code generating the issue.
Looks like ok, but I may advise looking at the Sequence.js:52:14, you can even debug it.
Found the issue - it was related to myself in a later stage not passing callback on, which was causing some issue in SLS-offline package logging an error into the mysql connection. Solved it now :) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.