【问题标题】:Why am I getting "createProperty is not a function"?为什么我得到“createProperty 不是函数”?
【发布时间】:2021-01-13 17:34:49
【问题描述】:

我是 Azure 和 Azure Bot Framework 的新手。我正在启动我的基本机器人(使用 Azure 本身生成,并进行了一些调整以简化机器人以便之后更好地理解),但在 npm start 之后出现此错误:

TypeError: userState.createProperty is not a function

我已经搜索了 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

编辑:

为了提供更多上下文,我使用this 对话框作为参考来实现我的主对话框,而这里没有userState.createProperty&lt;PropertyAccessor&gt;,这就是为什么我不知道它是否有必要,所以我会给它一个试试看。

关于在哪里可以找到userState,这是代码(它在不同的文件中,错误在我的mainDialog.jsuserState 来自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);
    });
});

【问题讨论】:

  • 分享mainDialog.js第36行左右的文件
  • mainDialog.js 的第 36 行是 this.userProfileAccessor = userState.createProperty(USER_PROFILE_PROPERTY);
  • @SimonaPentangelo - 你还没有告诉我们userState 的来源。它是什么类型的对象?它是如何创建的?您可以链接到您关注的任何示例吗? (由于这个帖子里还有很多人,如果你想让我看到你的回复,你需要@提及我。)
  • @KyleDeaney 我更新了我的问题,添加了更多代码和我正在使用的代码参考。现在也没有关于@(我的错),所以谢谢!
  • @SimonaPentangelo - 第二次声明const userState时真的不会报错吗?

标签: javascript node.js azure botframework bots


【解决方案1】:

JavaScript 异常“不是函数”在尝试从函数调用值但该值实际上不是函数时发生。

请注意UserState.CreateProperty返回StatePropertyAccessor接口。因此,this.userProfileAccessor 应该是 StatePropertyAccessor 的实现,您的函数调用应该是:userState.createProperty&lt;PropertyAccessor&gt;(USER_PROFILE_PROPERTY);

【讨论】:

  • 这有帮助吗?
  • @singhh-sdft 我已经更新了我的问题,无论如何我会尝试这个,如果它有效,我会在此处提供另一个更新,否则在尝试这个时会有其他问题。
  • 当然。请使用调试器并分享见解,以防它不起作用
猜你喜欢
  • 2022-01-02
  • 2021-12-13
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
相关资源
最近更新 更多