thingworx-field-work / ThingworxVSCodeProject

Develop thingworx models using a real IDE
MIT License
33 stars 17 forks source link

Lambda functions in services are not correcly translated to javascript #65

Closed kklorenzotesta closed 5 months ago

kklorenzotesta commented 7 months ago

When a lambda function is used inside a service, this is not translated to _this. For example from a file:

@ThingDefinition
class BugThing extends GenericThing {
  testLambda() {
    [0].forEach((_) => {
      logger.error(this.name);
    });
  }
}

The service inside the thing is generated with the following code:

var result = (function () {const METHOD_NAME = "testLambda";

    var _this = this;
    [0].forEach(function (_) {
        logger.error(this.name);
    });
}).apply(me, [])

which in this example leads to printing undefined since this doesn't refers correctly to the thing

BogdanMihaiciuc commented 7 months ago

I am aware of this but I haven’t yet figured out any good fix for it. A workaround you can use in the meantime is to alias this yourself and reference it that way.

BogdanMihaiciuc commented 5 months ago

I published version 2.1.5 of transformer and cli to npm, resolving this. Thank you for the fix