Closed JijeshP closed 5 years ago
Can you copy/paste some code snippet?
@slvnperron
const doSomeAction = async () => {
try {
user.somevalue = "test" // works fine
temp.somevalue = "test" // works fine
bot.somevalue = "test" // not working
} catch (error) {
}
}
return doSomeAction();
@JijeshP thanks, on it
@slvnperron Just wanted to store some variable across all users.
@JijeshP bug confirmed, in the meantime for a patch you can use the bp.kvs
to store stuff globally
Thank you @slvnperron
Use the KVS (bp.kvs
) inside the actions directly: https://botpress.io/reference/modules/_botpress_sdk_.kvs.html
@arnaldobadin
What the key-value-store does is saving your object in the database. Consequently, JS objects are serialized to valid JSON and stored into the database. As such, functions of any kind are cleared during the serialization (as it happens when you try to serialize a JS function with JSON.stringify) and thus the output of your await bp.kvs.get(botId, "engine");
looks reasonable to me.
What you want to do is to implement a registry for your functions. In BP 10, one could have attached the functions in a map to the bp object itself, something a long the lines of:
bp.engineStore[botId].engine = new Engine(); But no clue if that still works in BP 11...
But tell us more generally what exactly you want to achieve? It looks like you just want to reuse the "engine" instance right? To reuse the engine, you can make use of nodejs's modules, which <in general> are singletons. Since your Engine is already a node module, it's code is run on the first require and the cached object is returned on subsequenct requires to the exact same file.
/Crixx
Getting
bot is not defined
error when trying to store some value to bot. user and temp storage working fine.