【发布时间】: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<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);
});
});
【问题讨论】:
-
分享
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