You need to enable JavaScript to run this app. Azure Bot Framework启动报userState.createProperty非函数错误
优惠活动
大模型
产品
解决方案
定价
更多

Azure Bot Framework启动报userState.createProperty非函数错误

Azure Bot项目启动报错:TypeError: userState.createProperty is not a function

基本情况

  • 为Azure与Azure Bot Framework开发新手,基于Azure平台自动生成的基础Bot模板做了适当简化调整,准备后续深入理解开发逻辑,执行npm start命令启动项目时即出现运行错误
  • 已查阅Azure官方文档、技术社区等公开渠道的相关资料,暂未定位到问题解决方案

触发错误的核心代码

const USER_PROFILE_PROPERTY = 'USER_PROFILE_PROPERTY';
/* some code */
this.luisRecognizer = luisRecognizer;
this.userState = userState;
this.userProfileAccessor = userState.createProperty(USER_PROFILE_PROPERTY);

完整错误堆栈

C:\Users\Username\Desktop\botdirectory\dialogs\mainDialog.js:36
        this.userProfileAccessor = userState.createProperty(USER_PROFILE_PROPERTY);
                                             ^

TypeError: userState.createProperty is not a function
    at new MainDialog (C:\Users\Username\Desktop\botdirectory\dialogs\mainDialog.js:36:46)
    at Object.<anonymous> (C:\Users\Username\Desktop\botdirectory\index.js:82:16)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! core-bot@1.0.0 start: `node ./index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the core-bot@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Username\AppData\Roaming\npm-cache\_logs\2021-01-09T09_44_40_348Z-debug.log

补充排查信息

  • 参考开源项目中的dialogs/mainDialog.js文件实现自定义主对话逻辑时,注意到示例代码中未编写userState.createProperty<PropertyAccessor>的调用语句,因此最初未确认该调用的必要性,目前准备按照文档指引尝试补充对应逻辑排查问题
  • userState实例的来源:该实例在index.js文件中基于MemoryStorage初始化创建后,作为参数传入MainDialog构造函数,错误为mainDialog.js中调用createProperty方法时抛出
  • index.js中关联初始化代码如下:
/** all require are omitted **/

const memoryStorage = new MemoryStorage();
const conversationState = new ConversationState(memoryStorage);
const userState = new UserState(memoryStorage);

const { LuisAppId, LuisAPIKey, LuisAPIHostName } = process.env;
const luisConfig = { applicationId: LuisAppId, endpointKey: LuisAPIKey, endpoint: `https://${ LuisAPIHostName }` };

const luisRecognizer = new StreamAdvLuis(luisConfig);

const userState = new UserState(memoryStorage);
//My main dialog
const dialog = new MainDialog(luisRecognizer, userState);
//My bot
const bot = new DialogBot(conversationState, userState, dialog);

const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
    console.log(`\n${ server.name } listening to ${ server.url }`);
    console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
    console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});

// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
    // Route received a request to adapter for processing
    adapter.processActivity(req, res, async (turnContext) => {
        // route to bot activity handler.
        await bot.run(turnContext);
    });
});

// Listen for Upgrade requests for Streaming.
server.on('upgrade', (req, socket, head) => {
    // Create an adapter scoped to this WebSocket connection to allow storing session data.
    const streamingAdapter = new BotFrameworkAdapter({
        appId: process.env.MicrosoftAppId,
        appPassword: process.env.MicrosoftAppPassword
    });
    // Set onTurnError for the BotFrameworkAdapter created for each connection.
    streamingAdapter.onTurnError = onTurnErrorHandler;

    streamingAdapter.useWebSocket(req, socket, head, async (context) => {
        // After connecting via WebSocket, run this logic for every request sent over
        // the WebSocket connection.
        await bot.run(context);
    });
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.10 11:15:01