Javascript 无法读取属性';长度';bot框架中未定义错误的处理
我正在从Javascript 无法读取属性';长度';bot框架中未定义错误的处理,javascript,node.js,azure,botframework,Javascript,Node.js,Azure,Botframework,我正在从axiosget请求获取数据,并将其移动到数组xyz。但当我将xyz发送到step.prompt时,它抛出了以下错误: “[onTurnError]:TypeError:无法读取的属性'length'。” 未定义” 当我在日志中打印xyz时,它具有我需要的正确数据 async someFunction(step){ var xyz = []; try { const response = await axios.get(`url`); f
axios
get请求获取数据,并将其移动到数组xyz
。但当我将xyz
发送到step.prompt
时,它抛出了以下错误:
“[onTurnError]:TypeError:无法读取的属性'length'。”
未定义”
当我在日志中打印xyz
时,它具有我需要的正确数据
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);
}
async someFunction(步骤){
var xyz=[];
试一试{
const response=wait axios.get(`url`);
对于(var i=0;i
我想将
xyz
中的元素作为提示发送给用户。对于要使用提示的bot框架,您可以使用如下内容
提示输入尺寸验证样本
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 },
});
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'],
});
}
位置选择示例提示
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 },
});
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'],
});
}
我假设,您的第二个参数应该是任何数组/列表,而您传递的是名为“Choose any one”的字符串,这就是为什么它会给出“Cannot read property'length'of undefined”,因为它必须尝试访问数组的第一个或第二个元素,而传递的参数是一个字符串
prompt方法的第二个参数采用prompt options对象,该对象具有以下属性
作为参考,您可以在下面的文档中详细阅读
希望对您有所帮助。对于要使用prompt的bot框架,您可以使用以下内容
for (var i = 0; i < response.data.length; i++) {
xyz[i] = `${response.data[i].xzyElement}`;
}
提示输入尺寸验证样本
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 },
});
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'],
});
}
位置选择示例提示
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 },
});
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'],
});
}
我假设,您的第二个参数应该是任何数组/列表,而您传递的是名为“Choose any one”的字符串,这就是为什么它会给出“Cannot read property'length'of undefined”,因为它必须尝试访问数组的第一个或第二个元素,而传递的参数是一个字符串
prompt方法的第二个参数采用prompt options对象,该对象具有以下属性
作为参考,您可以在下面的文档中详细阅读
希望有帮助。for(var i=0;ifor (var i = 0; i < response.data.length; i++) {
xyz[i] = `${response.data[i].xzyElement}`;
}
xyz[i]=`${response.data[i].xzyElement}`;
}
尝试以上述格式将任何元素值添加到数组中
这样就不会出现类型错误
for(var i=0;i
尝试以上述格式将任何元素值添加到数组中
这样就不会出现
类型错误
我用这个密码试过了。return wait stepContext.prompt(位置提示,{prompt:'请选择位置',retryPrompt:'抱歉,请从列表中选择位置',选项:['Redmond','Bellevue','sattle'],});但在我的例子中,选择值应该从动态数组中获得,那个时我遇到了长度问题。如果我用值在函数中本地声明它,那么就没有问题。我用这段代码进行了尝试。return wait stepContext.prompt(位置提示,{prompt:'请选择位置',retryPrompt:'抱歉,请从列表中选择位置',选项:['Redmond','Bellevue','sattle'],});但在我的例子中,选择值应该从动态数组中获得,那个时我遇到了长度问题。如果我用值在函数中本地声明它,那么就没有问题。