javascript - 为什么我得到 "createProperty is not a function"?

标签 javascript node.js azure botframework bots

我是 Azure 和 Azure Bot 框架的新手。我正在启动我的基 native 器人(使用 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! <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385b574a5d155a574c780916081608" rel="noreferrer noopener nofollow">[email protected]</a> start: `node ./index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e5e9f4e3abe4e9f2c6b7a8b6a8b6" rel="noreferrer noopener nofollow">[email protected]</a> 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<PropertyAccessor> ,这就是为什么我不知道有必要,所以我会尝试一下。

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

最佳答案

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

请注意 UserState.CreateProperty返回StatePropertyAccessor界面。因此,this.userProfileAccessor应该是 StatePropertyAccessor 的实现你的函数调用应该是这样的: userState.createProperty<PropertyAccessor>(USER_PROFILE_PROPERTY);

关于javascript - 为什么我得到 "createProperty is not a function"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65641287/

相关文章:

node.js - NodeJS - 服务无法使用 mongo -

javascript - 如何通过使用 PHP、javascript 和 html 单击从数据库检索的数据来编辑每行属性

javascript - 使用 FabricJS 克隆组的问题(复制和粘贴)

javascript - Node js 中的信号量等效,变量在并发请求中被修改?

azure - Azcopy:是否可以使用新的 SAS key 恢复下载

Azure Web应用程序的状态为 "AdminDisabled"

azure - 无法通过 docker 登录到 azure 容器注册表

javascript - if 条件在数组迭代中始终返回 true

javascript - Node.js 示例代码不起作用

javascript - 获取node.js中最后一个插入id并将其传递给第二个查询