Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

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

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: Valid array

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

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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...