Below is my lambda function. A request is coming from API
( API Gateway
). Even when I tried to pass the values directly to update table , its not updating. I am able to INSERT
data into the same table through API
. Just wondering what's wrong here.
Appreciate any help. Thanks in advance.
var mysql = require('mysql');
var config = require('./config.json');
var sql,response;
exports.handler = function(event, context) {
var connection = mysql.createConnection({
host : config.dbhost,
user : config.dbuser,
password : config.dbpassword,
database : config.dbname
});
exports.handler = function(event, context,callback) {
//prevent timeout from waiting event loop
context.callbackWaitsForEmptyEventLoop = false;
var variable1= event.variable1;
var variable2= event.variable2;
var id= event.id;
connection.query('UPDATE LocationData SET latitude = ?,longitude =? WHERE userId = ?', [variable1, variable2,id], function(error, results, fields) {
response = {};
response['id'] = results.id;
response['variable1'] = results.variable1;
response['variable2'] = results.variable2;
context.succeed(response);
});
};
};
You are missing the call to the connect
function. A working example would look like this:
var mysql = require('mysql');
var config = require('./config.json');
exports.handler = function(event, context) {
var connection = mysql.createConnection({
host : config.dbhost,
user : config.dbuser,
password : config.dbpassword,
database : config.dbname
});
connection.connect(); // <--- MISSING THIS!
exports.handler = function(event, context,callback) {
context.callbackWaitsForEmptyEventLoop = false;
var variable1 = event.variable1;
var variable2 = event.variable2;
var id = event.id;
var sql = 'UPDATE LocationData SET variable1=?,variable2=? WHERE userId=?';
connection.query(sql, [variable1, variable2, id], function(error, results, fields) {
context.succeed({
id: results.id,
variable1: results.variable1,
variable2: results.variable2
});
});
};
};
Also, as a general advice, you should always check the error variable in the callback, in case something is going wrong, and react accordingly to it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.