【发布时间】:2020-10-15 03:57:55
【问题描述】:
场景:
我有一个支持 Alexa Skill 的 Node.JS 12.x Lambda。用户向技能提出问题,搜索参数被发送到我的 Lambda 插槽中。我使用 mssql 包查询 SQL DB,然后将结果返回给用户。
问题:
如果我从数据库中获取结果,Lambda 会成功执行查询并返回结果,但是 Lambda 会超时,Alexa 似乎没有收到响应。如果我删除数据库查询并只返回一个字符串,一切正常。
怀疑:
我认为这里的 async/await 内容可能存在一些问题。我只是无法弄清楚问题是什么。我已经检查了我多次使用 async/await
如果我遗漏了什么,请告诉我。提前致谢!
代码:
/**
* Intent handler for FindSomething intent
*/
const MyHandler = {
/**
* Determine whether this handler is able to process this input
* @param {Object} handlerInput The input object
*/
canHandle(handlerInput) {
// This part works fine
return util.checkIntentMatch(handlerInput, INTENT_NAME);
},
/**
* Handle the input
* @param {Object} handlerInput The input object
*/
async handle(handlerInput) {
// Extract slot values
const [
searchTerm,
] = [
Alexa.getSlotValue(handlerInput.requestEnvelope, 'search_term'),
];
// Fulfill request
const responseText = await getResponseText(searchTerm);
// Respond
return handlerInput.responseBuilder
.speak(responseText)
.getResponse();
},
};
然后getResponseText 看起来像这样:
/**
* Get the response text for the query
* @param {string} searchTerm The search term from the user
*/
async function getResponseText(searchTerm) {
let sectorName = await getSectorForTerm(searchTerm);
console.log(`Inside getResponseText. sectorName: ${sectorName}`);
if (!sectorName) return format(NOT_FOUND_LANGUAGE, { searchTerm });
return format(FOUND_LANGUAGE, { searchTerm, sectorName });
}
/**
* Find the sector for a search term
* @param {string} searchTerm The search term from the user
*/
async function getSectorForTerm(searchTerm) {
// ========================================================================
// If I uncomment this line, it works great -- all the way back to the user
// ========================================================================
//return 'fake result';
// Gather prerequisites in parallel
let [
query,
pool
] = await Promise.all([
fs.readFile(path.join(__dirname, 'queries', 'FindQuery.sql'), 'utf8'),
sql.connect(process.env['connectionString'])
]);
console.log('Pre query');
// Run query
let queryResult = await pool.request()
.input('EntityName', sql.TYPES.VarChar, searchTerm)
.query(query);
console.log('Post query');
// Extract result if any
let result = undefined;
if(queryResult.recordset.length > 0) {
result = queryResult.recordset[0]['SectorName'];
}
console.log(`result of getSectorForTerm: ${result}`);
return result;
}
编辑:
这是日志的样子。您可以看到文件已加载,查询已执行,并且在 ~500 毫秒内命中了返回语句。然后在函数超时之前经过几秒钟。
编辑 2:
我已经像 AWS 文档中的这个示例一样构建了我的 index.js,因此我无法直接访问 context 或类似内容。如果需要,可以更改。
【问题讨论】:
标签: alexa-skills-kit alexa-skill