P粉9605255832023-08-26 13:59:33
问题在于db.query
使用回调样式而不是promise样式。你需要将其转换为使用promises。
一种方法是使用Util.promisify
...
const util = require('util'); const mysql = require('mysql'); const db = mysql.createConnection({ host: "*****", port: *****, user: "*****", password: "*****", database: "*****", }); // 将db.query转换为返回promise的函数 const promisifiedQuery = util.promisify(db.query); export const lambdaHandler = async (event: any, context: any,) => { const user = { uid: event.request.userAttributes.sub, username: event.request.userAttributes.nickname, email: event.request.userAttributes.email, }; console.log("before query"); // 顺便说一下,这样做是不好的。请阅读有关如何避免SQL注入的内容。 const query = `INSERT INTO users (Uid, Username, Email) VALUES (${user.uid}, ${user.username}, ${user.email})`; await promisifiedQuery(query).then(result => { console.log("query response: " + result) console.log("after query") }).catch(console.error) return event; };