问题
I am getting the data from axios
get request, and moving it to an array xyz
. But when I am sending xyz
to step.prompt
, it is throwing this error:
" [onTurnError]: TypeError: Cannot read property 'length' of undefined"
When I print the xyz
in log, it has the proper data which I need.
async someFunction(step){
var xyz = [];
try {
const response = await axios.get(`url`);
for (var i = 0; i < response.data.length; i++) {
xyz[i] = response.data[i].xzyElement;
}
} catch (error) {
console.log(`error ${error}`);
}
return await step.prompt(PROMPT, 'Choose any one.', xyz);
}
I want to send the elements in xyz
as a prompt to the user.
回答1:
For a bot framework to use prompt you could use something like below
Prompt for size validation sample
return await stepContext.prompt(
SIZE_RANGE_PROMPT, {
prompt: 'How many people is the reservation for?',
retryPrompt: 'How large is your party?',
validations: { min: 3, max: 8 },
});
prompt for location selection sample
async promptForLocation(stepContext) {
// Record the party size information in the current dialog state.
stepContext.values.size = stepContext.result;
// Prompt for location.
return await stepContext.prompt(LOCATION_PROMPT, {
prompt: 'Please choose a location.',
retryPrompt: 'Sorry, please choose a location from the list.',
choices: ['Redmond', 'Bellevue', 'Seattle'],
});
}
I am assuming ,your second param should be any array/list , instead you are passing string named "Choose any one" which is why it is giving " Cannot read property 'length' of undefined" as it must be trying to access first or second element of the array and passed param is a string.
The second parameter of the prompt method takes a prompt options object, which has the following properties.
For reference , you can read in details in below doc
https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=javascript
Hope it helps.
回答2:
for (var i = 0; i < response.data.length; i++) {
xyz[i] = `${response.data[i].xzyElement}`;
}
Try to add any element value into array in the above format.
Then the TypeError
will not occur.
来源:https://stackoverflow.com/questions/55550764/cannot-read-property-length-of-undefined-error-in-bot-framework