在 Microsoft Bot Framework session.conversationData.array 中,正则表达式在执行一次后更改为对象类型的数组
In Microsoft Bot Framework session.conversationData.array of regexes changes to array of Object type after executing once
我正在使用 Microsoft Bot Framework 中的以下代码来访问全局命令的正则表达式列表。此代码是 botbuilder 模块的一部分:
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));
这里的问题是,代码第一次运行良好,分配给变量 session.conversationData.globalCommands 的值的形式如下:
但是,在第一次执行后,数组转换为以下内容,而其他任何地方的代码均未进行任何更改:
因此,行:
var isGlobalCommand = session.conversationData.globalCommands.some(regex => regex.test(session.message.text));
抛出异常 "regex.test is not a function"。
我无法理解为什么会发生这种情况以及如何解决这个问题,因为我需要单独使用此列表进行某些处理。
我相信您不能在机器人商店中存储复杂类型(conversationData 等)。该对象需要可序列化为 JSON,但我不认为它是 RegExp。
解决方法是将正则表达式存储为字符串,然后使用构造函数和存储的字符串表达式重新创建正则表达式对象。
查看 core-State 示例以了解有关商店功能的更多信息。
我正在使用 Microsoft Bot Framework 中的以下代码来访问全局命令的正则表达式列表。此代码是 botbuilder 模块的一部分:
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));
这里的问题是,代码第一次运行良好,分配给变量 session.conversationData.globalCommands 的值的形式如下:
但是,在第一次执行后,数组转换为以下内容,而其他任何地方的代码均未进行任何更改:
因此,行:
var isGlobalCommand = session.conversationData.globalCommands.some(regex => regex.test(session.message.text));
抛出异常 "regex.test is not a function"。
我无法理解为什么会发生这种情况以及如何解决这个问题,因为我需要单独使用此列表进行某些处理。
我相信您不能在机器人商店中存储复杂类型(conversationData 等)。该对象需要可序列化为 JSON,但我不认为它是 RegExp。
解决方法是将正则表达式存储为字符串,然后使用构造函数和存储的字符串表达式重新创建正则表达式对象。
查看 core-State 示例以了解有关商店功能的更多信息。