Java Script Write to HMI tags

Java Script Write to HMI tags
none 5.0 1
  • HMI Model: cMT-FHDX-820
  • EasyBuilder Pro Version: V6.09.01.556 Build 2024.04.16

I have been successful in creating a JavaScript Object based on tutorials, and everything works except for writing results to a tag for use outside of the script. For troubleshooting I have created an object which ONLY writes to a tag, and I still have not had success.

config:
Name: value, Type: Address, Value: LB-1 Bit

Source code:
driver.setData(this.config.value, 1)

Fault in cMT Diagnoser:
"TypeError: cannot read property ‘config’ of undefined at (:1)

I have tried many variations of this, but most give this fault.

Hi @unicyclingengineer ,

Thank you for your question. There are a few troubleshooting steps we can try to resolve the issue. Could you try assigning this to a local variable and referencing the local variable instead? For example,

var self = this;
var data = [1]; // define data as an array
driver.setData(self.config.value, data);

Please note that setData expects the value to be passed as an array (or array-like structure), even when writing a single value.

driver.setData(self.config.value, [1]); 

If the issue persists after making these changes, you can add a console.log() statement to help verify what is available at runtime:

var self = this;
console.log(self);

This will help confirm whether the object and its config property are being initialized as expected.

Please let us know what the output shows, and feel free to share screenshots of your JavaScript object configuration so we can assist you further!

Switching to an array of one element worked for me, thanks! I used your example:

var self = this;
var data = [1]; // define data as an array
driver.setData(self.config.value, data);