Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to Azure and Azure Bot Framework. I was starting my basic bot (generated with Azure itself and with some adjustments to simplify the bot to have a better understanding afterwards), but after npm start I got this error:

TypeError: userState.createProperty is not a function

I've searched on Azure documentation, here and wherever I could, but I didn't find a solution yet. Here is the code that caused the error:

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

And here is the full error trace:

C:UsersUsernameDesktopotdirectorydialogsmainDialog.js:36
        this.userProfileAccessor = userState.createProperty(USER_PROFILE_PROPERTY);
                                             ^

TypeError: userState.createProperty is not a function
    at new MainDialog (C:UsersUsernameDesktopotdirectorydialogsmainDialog.js:36:46)
    at Object.<anonymous> (C:UsersUsernameDesktopotdirectoryindex.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:UsersUsernameAppDataRoaming
pm-cache\_logs2021-01-09T09_44_40_348Z-debug.log

EDIT:

To give more context, I was using as reference this dialog to implement my main dialog, and here there's not userState.createProperty<PropertyAccessor>, that's why I didn't know it would be necessary, so I'll give it a try.

About where to find userState, here is the code (it's in a different file, the error is in my mainDialog.js, userState comes from 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(`
${ server.name } listening to ${ server.url }`);
    console.log('
Get Bot Framework Emulator: https://aka.ms/botframework-emulator');
    console.log('
To 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);
    });
});

question from:https://stackoverflow.com/questions/65641287/why-am-i-getting-createproperty-is-not-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
62 views
Welcome To Ask or Share your Answers For Others

1 Answer

The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function.

Please note that UserState.CreateProperty returns StatePropertyAccessor interface. Hence, this.userProfileAccessor should be an implementation of StatePropertyAccessor and your function call should be like: userState.createProperty<PropertyAccessor>(USER_PROFILE_PROPERTY);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...