无法从lambda更新mysql(aws rds)表,尽管我可以连接并插入

rt4zxlrg  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(302)

下面是我的lambda函数。一个请求来自 API ( API Gateway ). 即使我试图将值直接传递给update表,它也不会更新。我可以 INSERT 数据通过 API . 只是想知道这里怎么了。
谢谢你的帮助。提前谢谢。

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);
    });

};
};
5m1hhzi4

5m1hhzi41#

你错过了呼叫 connect 功能。工作示例如下所示:

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
      });
    });
  };
};

另外,作为一般建议,您应该始终检查回调中的error变量,以防出现问题,并对其做出相应的React。

相关问题