I am creating a D2 ui node and having some problems with the server side data store.
Following the docs and the example ui node I have, in onInput()
in the node's js file
// store the latest value in our Node-RED datastore
base.stores.data.save(base, node, msg)
// send it to any connected nodes in Node-RED
send(msg)
Then in the .vue file I have
mounted () {
this.$socket.on('widget-load:' + this.id, (msg) => {
...
So when the browser connects or is refreshed it first receives the new config properties, then it receives a message containing the current data store. This works OK.
The problem I have is that if I edit the node and deploy then connected browsers receive the new config properties, but are also sent the message with the old data store contents, which may not be compatible with the new config.
I had expected that the server side data store would be cleared when a node is deployed, which appears not to be the case. So question 1. - Is this a bug or is there a reason for it?
The solution I came up with was to manually clear out the datastore when the node is recreated, so I added code to that in the .js file so that I now have
RED.nodes.createNode(this, config)
const node = this
// which group are we rendering this widget
const group = RED.nodes.getNode(config.group)
const base = group.getBase()
// clear the server side data store, this ensures it is cleared if the node is deployed
console.log(`clearing server data store`)
base.stores.data.save(base, node.id, {})
...
and I can see that is running as the console.log message appears in the log, but still the browser receives the old message with the invalid data. So question 2 is what am I doing wrong?