ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

Regex for ID in subscribe does not work

Open pedrr opened this issue 5 years ago • 7 comments

Hi,

i'd like to use a Regex in my javascript on subscribe part

i have the following code:

on({ id: /^hm-rpc.\d..+.SET_TEMPERATURE$/, },

This gives me the following javascript error in the logs.

javascript.0 2020-03-27 07:12:53.074 error (20461) at ContextifyScript.Script.runInContext (vm.js:59:29)
javascript.0 2020-03-27 07:12:53.074 error (20461) at script.js.common.notifiy-onTempChange:1:1
javascript.0 2020-03-27 07:12:53.072 error (20461) at on (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1200:28)
javascript.0 2020-03-27 07:12:53.071 error (20461) at Object.subscribe (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1130:13)
javascript.0 2020-03-27 07:12:53.069 error (20461) at subscribePattern (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:101:29)
javascript.0 2020-03-27 07:12:53.068 error (20461) at Adapter.getForeignStates (/opt/iobroker/node_modules/iobroker.js-controller/lib/adapter.js:5920:43)
javascript.0 2020-03-27 07:12:53.067 error (20461) TypeError: pattern.replace is not a function
javascript.0 2020-03-27 07:12:53.065 error (20461) ^
javascript.0 2020-03-27 07:12:53.064 error (20461) startkey: pattern.replace(/*/g, ''),
javascript.0 2020-03-27 07:12:53.063 error (20461) script.js.common.notifiy-onTempChange: /opt/iobroker/node_modules/iobroker.js-controller/lib/adapter.js:5920

pedrr avatar Mar 27 '20 06:03 pedrr

which js-controller version you have running?

Apollon77 avatar Mar 27 '20 07:03 Apollon77

Reason is basically that javascript adapter uses getForeignStates to initialize current values, but this method do not support regex as id ...

Apollon77 avatar Mar 27 '20 07:03 Apollon77

I'm running 4.3.4 I found the issue on my own. Regex only works if you do not select "Do not subscribe on all states on startup". This was reproducible in my case. Can someone confirm this?

pedrr avatar Mar 27 '20 08:03 pedrr

my infos above is kind of an confirmation. It can not work that way "code wise"

Apollon77 avatar Mar 27 '20 08:03 Apollon77

Why you close it? Needs to be fixed

Apollon77 avatar Mar 27 '20 14:03 Apollon77

Why you close it? Needs to be fixed

It is not a bug it is new feature.

GermanBluefox avatar May 23 '20 08:05 GermanBluefox

It is because of https://github.com/ioBroker/ioBroker.js-controller/blob/master/lib/adapter.js#L6872

                // read first the keys for pattern
                let params = {};
                if (pattern && pattern !== '*') {
                    params = {
                        startkey: pattern.replace(/\*/g, ''),
                        endkey:   pattern.replace(/\*/g, '\u9999')
                    };
                }

GermanBluefox avatar May 23 '20 08:05 GermanBluefox