openhab-helper-libraries icon indicating copy to clipboard operation
openhab-helper-libraries copied to clipboard

ReferenceError: "me" is not defined in triggers.js

Open Martin-Stangl opened this issue 6 years ago • 6 comments

Full error message: Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/javascript/personal/Papierschredder.js': ReferenceError: "me" is not defined in /etc/openhab2/automation/lib/javascript/core/triggers.js at line number 158

I did not define me in Papierschredder.js, as I don't use it anywhere. So far getTrName in triggers.js seems to be the only place demanding it. Currently I found 2 workarounds:

  1. Globally define me, for e.g.: var me = __FILE__.split(__DIR__).pop();

  2. Provide a triggerName with each trigger.

My solution suggestions:

  1. Either just use the uuid if no triggerName was provided. If someone wants to be able to identify the source of their triggers, they have to do the works and provide triggerNames.

  2. Use the rule name instead of me. Probably needs to be done in JSRule then instead of getTrName: Go through all triggers in the JSRule function and if a trigger has no triggerName, generatge it there with uuid and name.

Martin-Stangl avatar Oct 12 '19 00:10 Martin-Stangl

Not directly related to the issue, but two corrections to my workaround 1:

  1. __FILE__.split(__DIR__).pop() works on Linux, but not on Windows, as FILE always uses / as path separator, but DIR uses \ on Windows. Therefore __FILE__.split('/').pop() needs to be used.

  2. __FILE__.split('/').pop() works only for files loaded in with load(). The initial .js seems to be loaded using a reader. therefore FILE contains <eval> and not the file name. So it seems to be no clever way around to defining something like me = "myrule.js" for logging purposes. Still, the usage of me in getTrName is not clean and should be fixed.

Actually, I consider to give it a go myself to implement my solution suggestion 2, but I am a little unsure on how JSRuleNew fits into the picture. Is this just a work in progress of a new implementation of JSRule? Then I would need to implement my fix in both.

Martin-Stangl avatar Oct 15 '19 11:10 Martin-Stangl

Thank you for reporting the problem and the analysis. The JS libraries will be completely reworked to match the Jython libraries once ES6 is available (OH JDK9 compatibility). If you want to clean them up, go ahead, otherwise I'll get in there at some point, but my time is very limited ATM.

5iver avatar Oct 15 '19 14:10 5iver

Time is also an issue for me, but I feel motivated to find some currently and give it a try.

Martin-Stangl avatar Oct 16 '19 15:10 Martin-Stangl

Just a heads up... you might want to look into #267. @jpg0 is proposing something that would likely cleanup this issue.

5iver avatar Oct 16 '19 15:10 5iver

I am no JS expert, but if I understand it right, it would force us to fix this issue, as there would be no way to access me from within getTrName anymore.

Martin-Stangl avatar Oct 16 '19 16:10 Martin-Stangl

FYI this did indeed force it to be fixed. (Well, you could probably explicitly assign the name to global.triggerName, but, let's not.) I just used the uuid if there was no name.

jpg0 avatar Nov 27 '19 00:11 jpg0