gpt4 book ai didi

javascript - Node.JS MySQL 连接超时

转载 作者:行者123 更新时间:2023-11-29 17:58:13 25 4
gpt4 key购买 nike

我试图让我的 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 秒并再次测试。

enter image description here

编辑:如果您没有看到任何 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();

};

关于javascript - Node.JS MySQL 连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48629831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com