[英]Node.JS MySQL Connection Timing Out
我正在尝试让我的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
不知道为什么要花7秒钟才能做出响应,但是您需要确保“基本设置”下的Lambda超时大于查询所花费的时间。 我将其设置为10秒,然后再次测试。
编辑:如果您没有看到任何console.log消息,则说明它没有执行代码。 尝试以下代码,看看是否可以从MySQL获得任何响应并在日志中验证响应。 此代码假定您已经为MySQL打包了必要的库,并将其上载到Lambda函数。
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(); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.