mysql - 无法将 AWS lambda 连接到 mysql 数据库
问题描述
嗨,我似乎无法将我的 sql 数据库连接到我的 lambda 函数。以下是我尝试使用的代码。我已经为我的 package.json 添加了一个层。我将我的 lambda 函数链接到 amazon lex,但是当我尝试打印 ${connection.state} 时,它抛出了这个错误“发生错误:无效的 Lambda 响应:收到来自 Lambda 的错误响应:未处理”。我还添加了必要的 lambda 权限。
感谢您的帮助谢谢!
function dispatch(intentRequest, callback) {
const sessionAttributes = intentRequest.sessionAttributes;
const slots = intentRequest.currentIntent.slots;
const mysql = require("mysql");
var connection = mysql.createConnection({
host : 'xxx',
user : 'admin',
password : 'xxx',
database : 'xxx',
port : 3306
});
exports.handler = (event, context) => {
connection.connect(function(err) {
if (err) context.fail();
else context.succeed('Success');
});
};
callback(close(sessionAttributes, 'Fulfilled',
{'contentType': 'PlainText', 'content': `Thank you ${connection.state}`}));
}
// Route the incoming request based on intent.
// The JSON body of the request is provided in the event slot.
exports.handler = (event, context, callback) => {
try {
dispatch(event,
(response) => {
callback(null, response);
});
} catch (err) {
callback(err);
}
}
解决方案
推荐阅读
- c - 尝试阅读 cp.c 的代码但不理解
- java - ViewPager:如果用户滑动低于或等于屏幕的 50%,则不会调用 onPageSelected
- python - 子图中的 wspace 和 hspace,轴的宽度 - python3
- node.js - 使用 Node.js 迭代对象数组,如果值匹配,则将所有相关值插入不同的数组
- git - 如何使用 GIT 进行语义版本控制?
- bash - 为什么分支似乎不是 git 存储库?它不是主分支
- typescript - yarn/npm 莫名其妙地在 mocha 和 typescript 中使用过时的代码
- xcode - macOS 应用程序可以更改自己的环境变量吗?
- node.js - 续集 findAndCountAll 并计算包含的模型
- laravel - 如何从laravel中的角度get方法获取参数?