Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

node.js - 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.

(我需要使dialogflow响应并返回从电子表格中获取的数据。)

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 is "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 = myCode) 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!

(我将非常感谢您的帮助!)

  ask by usama sulaiman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...