I'm currently exploring the use of Typescript with Firebase Functions, aiming to define a class with a method that I want to pass as a callback for exporting the function code. My initial attempt looked like this:
class Bar {
baz (data) { return data.baz; }
handler(event) { return this.baz(event.data); }
}
In my index.ts file, I tried the following approach:
functions.pubsub.topic('subscriptions').onPublish(new Bar().handler);
Unfortunately, it resulted in an error stating
cannot access property 'baz' of undefined
, indicating that this
is not defined within the handler
method.
To address this issue, I attempted using the call method like so:
const barInstance = new Bar()
functions.pubsub.topic('subscriptions').onPublish((event) => barInstance.handler.call(barInstance, event));
Despite trying this workaround, I encountered the same error message. At this point, I am at a loss. Any suggestions would be greatly appreciated.