Currently, I am working on constructing an AWS Serverless function using TypeScript.
My focus is on creating an abstract class with a single public method that invokes some private methods.
Below is the simplified version of my TypeScript class:
export abstract class AbstractPayloadProcessor {
private preHandlePayload(payload) {
console.log('arrived in prehandle method')
}
private postHandlePayload(payload) {
console.log('arrived in posthandle method')
}
protected handleException(error) {
console.log('Exception has occurred:', error)
}
public processPayload(event) {
// this line causes the issue
this.preHandlePayload(event.payload)
.
.
.
}
}
To transpile this code into ES5, I use webpack which generates the following JavaScript output:
var AbstractPayloadProcessor = /** @class */ (function () {
AbstractPayloadProcessor.prototype.preHandlePayload = function (payload) {
console.log('arrived in prehandle method');
};
AbstractPayloadProcessor.prototype.postHandlePayload = function (payload) {
console.log('arrived in posthandle method');
};
AbstractPayloadProcessor.prototype.handleException = function (error) {
console.log('Exception has occurred:', error);
};
AbstractPayloadProcessor.prototype.processPayload = function (event) {
// this line throws the exception
this.preHandlePayload(event.payload);
.
.
.
}());
The specific exception encountered is:
TypeError: Cannot read property 'preHandlePayload' of undefined
I have attempted configuring webpack to utilize ES6 and declaring functions in TypeScript using arrow functions, but both approaches result in the same exception.
The processPayload
function gets triggered within the Serverless framework, though I am not familiar with its internal workings. If anyone has insights or experience with this framework, your input would be greatly welcomed.
Thank you for your attention.