【发布时间】:2019-06-03 19:35:28
【问题描述】:
这是我第一次使用 async/await。我在对话流意图内的数据库请求上下文中使用它时遇到问题。如何修复我的代码?
会发生什么?
当我尝试使用我的后端运行时 - 这是我得到的:“Webhook 调用失败。错误:请求超时。”
我怀疑什么?
我的辅助函数 getTextResponse() 等待 airtable 的返回值,但永远不会得到它。
我想做什么?
- “GetDatabaseField-Intent”被触发
- 在它内部通过 getTextResponse() 向我的 airtable 数据库发送请求
- 因为我使用了“await”,所以函数会等待结果再继续
- getTextResponse() 将返回“returnData”;因此 var 结果将填充“returnData”
- getTextResponse() 已完成;因此将使用它的返回值创建响应
'use strict';
const {
dialogflow
} = require('actions-on-google');
const functions = require('firebase-functions');
const app = dialogflow({debug: true});
const Airtable = require('airtable');
const base = new Airtable({apiKey: 'MyKey'}).base('MyBaseID');
///////////////////////////////
/// Helper function - reading Airtable fields.
const getTextResponse = (mySheet, myRecord) => {
return new Promise((resolve, reject) => {
// Function for airtable
base(mySheet).find(myRecord, (err, returnData) => {
if (err) {
console.error(err);
return;
}
return returnData;
});
}
)};
// Handle the Dialogflow intent.
app.intent('GetDatabaseField-Intent', async (conv) => {
const sheetTrans = "NameOfSheet";
const recordFirst = "ID_OF_RECORD";
var result = await getTextResponse(sheetTrans, recordFirst, (callback) => {
// parse the record => here in the callback
myResponse = callback.fields.en;
});
conv.ask(myResponse);
});
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
【问题讨论】:
标签: node.js async-await dialogflow-es actions-on-google airtable