简体   繁体   English

Dialogflow agent.add 不使用 promise

[英]Dialogflow agent.add not working with promise

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.我正在尝试从电子表格返回数据,代码工作正常,数据出现在日志中,但agent.add没有正常工作,正如预期的那样。

I need is to make the dialogflow to response and return the data which I get from spreadsheet.我需要的是使对话流响应并返回我从电子表格中获得的数据。

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:发生的事情是,在代码的一个位置,dialogflow agent.add按预期工作,但它没有返回我从电子表格中获取的数据,请参见这一部分:

agent.add("status code inside: "+ myCode); // agent.add working, but myCode variable not showing the data which returned from spreadsheet, it only shows the original value "XmXm"

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在代码的另一个位置, agent.add不起作用,即使我可以在日志中看到电子表格数据,(我在agent.add之前尝试过有和没有return )看到这部分

return agent.add("status code outside:"+ value); // function agent.add whether or not I add return before it

Note that I tried many solution which provided in stackoverflow, but I still can't fix it, below is my full code:请注意,我尝试了很多在 stackoverflow 中提供的解决方案,但我仍然无法修复它,下面是我的完整代码:

 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(){ // }

I'll be greatly appreciating your help!我将非常感谢您的帮助!

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经过多次尝试,我终于找到了答案,这很简单,我认为我需要在调用调用sheetPromiseHandelerGet(orderNum)的主函数sheetPromiseHandelerGet(detailsNum)之前添加return ,仅此而已

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
}

it must be肯定是

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

to be成为

resolve(agent.add(myCode));

I hope that can help anyone has the same problem我希望能帮助任何有同样问题的人

  • You want to use myCode retrieved from Spreadsheet with sheets.spreadsheets.values.get() at agent.add("status code inside: " + myCode);您想在agent.add("status code inside: " + myCode);中使用myCode sheets.spreadsheets.values.get()从 Spreadsheet 检索的 myCode . .
  • You want to achieve this using googleapis with Node.js.您想使用 googleapis 和 Node.js 来实现这一点。
  • You have already been able to get values for Google Spreadsheet with Sheets API.您已经能够使用 Sheets API 获取 Google Spreadsheet 的值。

If my understanding is correct, how about this answer?如果我的理解是正确的,那么这个答案呢? Please think of this as just on of several possible answers.请将此视为几个可能答案中的一个。

Modification points:修改点:

  • sheets.spreadsheets.values.get() works with the asynchronous process. sheets.spreadsheets.values.get()适用于异步过程。 By this, in your script, myCode of agent.add("status code inside: "+ myCode);这样,在你的脚本中, myCode of agent.add("status code inside: "+ myCode); is always the initial value (in your case, it's XmXm .).始终是初始值(在您的情况下,它是XmXm 。)。
  • In your script, sheets.spreadsheets.values.get is run in new Promise() .在您的脚本中, sheets.spreadsheets.values.getnew Promise()中运行。 So value of getSheetPromise.then(function(value) {}) is the retrieved value.所以 getSheetPromise.then(function( value getSheetPromise.then(function(value) {})的值是检索到的值。

Pattern 1:模式 1:

Modified script:修改脚本:

If you want to use agent.add("status code inside: "+ myCode);如果要使用agent.add("status code inside: "+ myCode); in new Promise in your script, how about the following modification?在你的脚本中的new Promise中,下面的修改怎么样?

From: 从:
 if (orderNum == data[i][0]){ myCode = `myCode: Order details: ${data[i][1]}`; resolve(myCode); }
To: 到:
let getSheetPromise = new Promise(async (resolve, reject) => {
  const mySpreadSheetId = "SHEET_ID";
  const sheetName = "SHEET_NAME";
  let myCode = "XmXm";
  // let testFirst = 0; // It seems that this is not used.
  const res = await sheets.spreadsheets.values.get({
    auth: jwtClient,
    spreadsheetId: mySpreadSheetId,
    range: `${sheetName}!A:H`
  });
  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);
      // break; // When there are several same values of `orderNum` in the column "A", please use this line.
    }
  }
  agent.add("status code inside: " + myCode);
});

Pattern 2:模式 2:

Modified script:修改脚本:

As other pattern, how about the following modification?作为其他模式,以下修改如何? In this case, please modify getSheetPromise in your script as follows.在这种情况下,请按如下方式修改脚本中的getSheetPromise

 let getSheetPromise = new Promise(async (resolve, reject) => { const mySpreadSheetId = "SHEET_ID"; const sheetName = "SHEET_NAME"; let myCode = "XmXm"; // let testFirst = 0; // It seems that this is not used. const res = await sheets.spreadsheets.values.get({ auth: jwtClient, spreadsheetId: mySpreadSheetId, range: `${sheetName}:A;H` }). 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); // break, // When there are several same values of `orderNum` in the column "A". please use this line. } } agent:add("status code inside; " + myCode); });
  • In this case, for example, when there are several same values of orderNum in the column "A", myCode of agent.add("status code inside: " + myCode) is the value of the last same row, while myCode of resolve(myCode) is the 1st same row.在这种情况下,例如当“A”列中有多个相同的orderNum值时, myCode agent.add("status code inside: " + myCode)的myCode是最后同一行的值,而resolve(myCode)myCode resolve(myCode)是第一行。 If you want to 1st one, please remove // of // break;如果你想要第一个,请删除// of // break; . .

try return agent.add("status code outside:"+value); try return agent.add("status code outside:"+value); I think '+' is missing.我认为缺少“+”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM