问题
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.
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 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
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:
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!
回答1:
- You want to use
myCode
retrieved from Spreadsheet withsheets.spreadsheets.values.get()
atagent.add("status code inside: " + myCode);
. - You want to achieve this using googleapis with Node.js.
- You have already been able to get values for Google Spreadsheet with Sheets API.
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. By this, in your script,myCode
ofagent.add("status code inside: "+ myCode);
is always the initial value (in your case, it'sXmXm
.).- In your script,
sheets.spreadsheets.values.get
is run innew Promise()
. Sovalue
ofgetSheetPromise.then(function(value) {})
is the retrieved value.
Pattern 1:
Modified script:
If you want to use agent.add("status code inside: "+ myCode);
in new Promise
in your script, how about the following modification?
if (orderNum == data[i][0]){
myCode = `myCode: Order details: ${data[i][1]}`;
resolve(myCode);
}
To:
if (orderNum == data[i][0]){
myCode = `myCode: Order details: ${data[i][1]}`;
agent.add("status code inside: "+ myCode);
resolve(myCode);
}
Pattern 2:
Modified script:
As other pattern, how about the following modification? In this case, please modify getSheetPromise
in your script as follows.
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
ofagent.add("status code inside: " + myCode)
is the value of the last same row, whilemyCode
ofresolve(myCode)
is the 1st same row. If you want to 1st one, please remove//
of// break;
.
回答2:
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
}
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
回答3:
try return agent.add("status code outside:"+value);
I think '+' is missing.
来源:https://stackoverflow.com/questions/59125963/dialogflow-agent-add-not-working-with-promise