In Microsoft Bot Framework session.conversationData.array of regexes changes to array of Object type after executing once

混江龙づ霸主 提交于 2020-01-17 06:53:39

问题


I am working with the below piece of code in Microsoft Bot Framework to access the list of regexes for global commands. This code is a part of botbuilder module:

if (typeof session.conversationData.globalCommands === "undefined") {
    // An array which contains the list of all global commands
    session.conversationData.globalCommands = [];
    // Accessing the list of global commands
    globalActions = session.library.actions.actions;
    lenGlobalActions = Object.keys(globalActions).length;
    // Assigning values to the above list
    for (var i=0; i<lenGlobalActions; i++){
        session.conversationData.globalCommands.push(globalActions[Object.keys(globalActions)[i]].options.matches);
    }
}
// Checking if the incoming message from the user is a global command
var isGlobalCommand = session.conversationData.globalCommands.some(regex => regex.test(session.message.text));   

The issue here is, the code runs fine for the first time and the values assigned to the variable session.conversationData.globalCommands are in the form given below:

However, after the first execution, the array converts to the below without any changes made in the code anywhere else:

Due to this, the line:

var isGlobalCommand = session.conversationData.globalCommands.some(regex => regex.test(session.message.text));   

throws an exception "regex.test is not a function".

I am unable to understand why this should be happening and how do I solve this as I need this list separately for some processing.


回答1:


I believe you cannot store complex types in the bot store (conversationData, etc). The object needs to be serializable to JSON and I don't believe a RegExp it is.

The workaround would be to store the regex as an string and then recreate the regex object using the constructor and the stored string expression.

Check the core-State sample to know more about the store capabilities.



来源:https://stackoverflow.com/questions/42926866/in-microsoft-bot-framework-session-conversationdata-array-of-regexes-changes-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!