You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure通信服务构建IVR遇TypeError: Cannot read properties of undefined (reading 'kind')求助

问题分析与解决方案

核心问题

你遇到的TypeError: Cannot read properties of undefined (reading 'kind')错误,主要来自两个方面:

  1. 手动构造的recognizeOptions普通对象不符合Azure Call Automation SDK的类型要求,SDK内部期望的是特定类的实例而非纯字面量对象。
  2. 代码中存在变量重复声明和属性名大小写错误的问题,进一步导致参数结构异常。

具体修复步骤

1. 修正变量重复声明与属性名

代码中两次使用const playSource声明变量属于语法错误;同时第二个音频源的属性名用了大写URL,而SDK要求小写url。修改如下:

if (eventType === 'Microsoft.Communication.CallConnected') {
    const callConnectionId = eventData.callConnectionId;

    // Play an audio 
    const initialPlaySource = { url: "...", kind: "fileSource" };
    await callAutomationClient.getCallConnection(callConnectionId).getCallMedia().playToAll([initialPlaySource]);

    // 修正变量名和属性名
    const promptPlaySource = { url: "...", kind: "fileSource" };

    // 后续代码...
};

2. 使用SDK提供的选项类创建识别配置

Azure Call Automation SDK要求startRecognizing方法的参数是特定类的实例(而非手动构造的字面量对象)。你需要导入并使用CallMediaRecognizeSpeechOptions类来创建识别选项,SDK会自动处理kind属性的正确赋值:

// 确保已导入该类
const { CallMediaRecognizeSpeechOptions } = require("@azure/communication-call-automation");

// ... 前面的代码 ...

const recognizeOptions = new CallMediaRecognizeSpeechOptions({
    playPrompt: promptPlaySource,
    initialSilenceTimeoutInSeconds: 10,
    interruptPrompt: true,
    operationContext: "OpenQuestionSpeech"
});

await callAutomationClient.getCallConnection(callConnectionId).getCallMedia().startRecognizing(recognizeOptions);

3. 验证SDK版本

确保你使用的是最新版的@azure/communication-call-automation包,旧版本可能存在类型处理的兼容性问题。可以通过以下命令更新:

npm update @azure/communication-call-automation

额外说明

手动构造字面量对象时,即使你手动指定了kind属性,SDK内部的类型校验逻辑仍可能无法识别该对象为合法的配置实例,从而导致读取kind时出现未定义错误。使用SDK提供的类实例是最可靠的方式。

内容的提问来源于stack exchange,提问作者Paria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:48:12