Node.JS MySQL连接时序输出

时间:2018-02-05 19:13:22

标签: javascript mysql node.js timeout alexa

我正在尝试让我的Alexa技能说出我的数据库查询结果,如下所示。但是,我收到超时错误,Alexa只是说:“请求的技能响应存在问题。”

此代码在AWS Lambda中运行并使用mySQL模块。

   'LaunchRequest': function () {
console.log('starting Launch');
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'bfdfdfm',
  user     : 'test',
  password : 'test',
  database : 'test',
  port     : '1433'
});

console.log('step2');

connection.connect();
console.log('step3');

connection.query('SELECT `book` FROM `dbo.tblBibleBooks` WHERE `id` = "4"', function (error, results, fields) {
  console.log('step4');
  if (error) console.log('did not work');
  console.log('The solution is: ', results);
  this.emit(':tell', results);
});

connection.end();

这是包含错误消息的控制台日志。值得注意的是,我没有记录任何控制台日志值!

2018-02-06T17:05:01.828Z    de66d608-0b5f-11e8-a70a-6f7ecc1bb68a    initializing
START RequestId: de66d608-0b5f-11e8-a70a-6f7ecc1bb68a Version: $LATEST
2018-02-06T17:05:02.184Z    de66d608-0b5f-11e8-a70a-6f7ecc1bb68a    starting Launch
2018-02-06T17:05:03.543Z    de66d608-0b5f-11e8-a70a-6f7ecc1bb68a    step2
2018-02-06T17:05:03.761Z    de66d608-0b5f-11e8-a70a-6f7ecc1bb68a    step3
END RequestId: de66d608-0b5f-11e8-a70a-6f7ecc1bb68a
REPORT RequestId: de66d608-0b5f-11e8-a70a-6f7ecc1bb68a  Duration: 10000.98 ms   Billed Duration: 10000 ms Memory Size: 128 MB   Max Memory Used: 45 MB  
2018-02-06T17:05:12.088Z de66d608-0b5f-11e8-a70a-6f7ecc1bb68a Task timed out after 10.00 seconds

2018-02-06T17:05:12.254Z    de66d608-0b5f-11e8-a70a-6f7ecc1bb68a    initializing
START RequestId: e4b89244-0b5f-11e8-9e15-6d418568d36a Version: $LATEST
END RequestId: e4b89244-0b5f-11e8-9e15-6d418568d36a
REPORT RequestId: e4b89244-0b5f-11e8-9e15-6d418568d36a  Duration: 176.56 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 33 MB  

1 个答案:

答案 0 :(得分:0)

不确定为什么需要7秒才能响应,但您需要确保“基本设置”下的Lambda超时大于查询所需的时间。我会将它设置为10秒并再次测试。

enter image description here

EDITED: 如果您没有看到任何console.log消息,则表明它没有执行代码。尝试以下代码,看看是否可以从MySQL获得任何响应并验证日志中的响应。此代码假定您已经打包并上传到Lambda函数的MySQL必需库。

const mysql = require('mysql');
const config = {
  host     : '.....amazonaws.com',
  user     : 'dafsdf',
  password : 'jasdf',
  database : 'asdf',
  port     : '1433'
}

exports.handler = (event, context, callback) => {
  
  var connection = mysql.createConnection(config);
  
    const sql = 'SELECT `book` FROM `dbo.tblBibleBooks` WHERE `id` = "4"'
    
    connection.connect();

    connection.query(sql, function(err, results, fields) {
      if (!err)
        {
          console.log('The solution is: ', results);
           this.emit(':tell', results);
        }
      else
        {
          console.log('Error while performing Query.');
          this.emit('error', err);
        }
    });
    
    connection.end();
  
};