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; };