Cannot read property 'length' of undefined error in bot framework

你离开我真会死。 提交于 2019-12-24 17:06:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!