Dialogflow agent.add无法与Promise一起使用 [英] Dialogflow agent.add not working with promise
问题描述
我正在尝试从电子表格中返回数据,代码工作正常,并且数据显示在日志中,但是 agent.add
不能正常工作。
I'm trying to return data from spreadsheet, the code is working fine, and data appears in the log, but agent.add
not working, as expected.
我需要做的是使对话框流响应并返回从电子表格中获取的数据。
I need is to make the dialogflow to response and return the data which I get from spreadsheet.
什么碰巧是在代码的某个位置,dialogflow agent.add
可以按预期工作,但是它没有返回我从电子表格中获取的数据,请参见以下部分:
What happen is that in one position of the code, the dialogflow agent.add
working as expected, but it's not returning the data which I get form the spreadsheet, see this part:
agent.add( status code inside: + myCode); // agent.add有效,但是
myCode变量不显示从电子表格返回的数据,它仅显示原始值 XmXm
在代码的其他位置, agent.add
不起作用,即使我可以在日志中看到电子表格数据, (我试过在 agent.add
之前有无返回
的情况)看到这一部分
In the other position of the code, agent.add
not working, even I can see the spreadsheet data in the log, (I tried with and without return
before agent.add
) see this part
return agent.add(外部状态码: +值); //函数
agent.add是否在其之前添加return
我尝试了许多stackoverflow提供的解决方案,但仍然无法解决,下面是我的完整代码:
Note that I tried many solution which provided in stackoverflow, but I still can't fix it, below is my full code:
function sheetPromiseHandelerGet(orderNum){
const jwtClient = authSheet();
let getSheetPromise = new Promise((resolve, reject) => {
const mySpreadSheetId = 'SHEET_ID';
const sheetName = "SHEET_NAME";
let myCode = "XmXm"; // give myCode a string value for testing
let testFirst = 0;
sheets.spreadsheets.values.get(
{
auth: jwtClient,
spreadsheetId: mySpreadSheetId,
range: `${sheetName}!A:H`
},
(err, res) => {
if (err) {
console.error(err);
return;
}
const data = res.data.values;
let i = 0;
for (i = 0; i < data.length; i++) {
if (orderNum == data[i][0]){
myCode = `myCode: Order details: ${data[i][1]}`;
resolve(myCode);
}
}
}
);
agent.add("status code inside: "+ myCode); // agent.add working, but myCode variable not is "XmXm"
});
return getSheetPromise.then(function(value) {
console.log(value); // the value (which is the result comes from spreadsheet) appears correctly in the debugger,
return agent.add("status code outside:" value); // function agent.add whether or not I add return before it
});
}
function orderStatusHandel(agent){
const detailsNum = agent.parameters.detailsNum;
sheetPromiseHandelerGet(detailsNum);
}
function authSheet(){
//
}
我会
推荐答案
最后,经过多次尝试,我找到了答案,这很简单,我认为我需要在调用主函数 sheetPromiseHandelerGet(detailsNum)
之前添加 return
,该函数调用 sheetPromiseHandelerGet(orderNum )
,仅此而已
Finally after many tries, I found the answer, which is very simple, I figure that I need to add return
before calling the main function sheetPromiseHandelerGet(detailsNum)
which calls the sheetPromiseHandelerGet(orderNum)
, that's all
因此,如果您希望在问题中结束代码,而不是
so if you look to end of my code in the question, instead of
function orderStatusHandel(agent){
const detailsNum = agent.parameters.detailsNum;
sheetPromiseHandelerGet(detailsNum); //return is missing
}
必须为
function orderStatusHandel(agent){
const detailsNum = agent.parameters.detailsNum;
return sheetPromiseHandelerGet(detailsNum); //return has been added
}
除此以外,最好进行其他更改:
Other than this, it's better to make other change:
resolve(myCode);
成为
resolve(agent.add(myCode));
我希望可以帮助任何遇到相同问题的人
I hope that can help anyone has the same problem
这篇关于Dialogflow agent.add无法与Promise一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!