0
broker.onTopic = function onTopic(topic: string, callback: Function): void {
    log.debug(`Callback to add: ${JSON.stringify(callback, null, 4)}`);
    //...more code

Somewhere else in my code:

interface ACPOSObject {
    data: State;
}
interface State {
    state: string;
}
broker.onTopic('[A-Z0-9]+/[A-Z0-9]+/[A-Z0-9]+/LINE/[A-Z]+/ACPOS', function onCenterline(
    topic: string,
    message: ACPOSObject
): void {
    log.debug(`New message in to topic: ${topic} message: ${message}`);

Logs show this:

2019-09-02T13:29:59.370Z debug: Callback to add: undefined

Why is the callback undefined?

basickarl
  • 37,187
  • 64
  • 214
  • 335

1 Answers1

0

Because JSON.stringify omits the functions by default.

See JSON.stringify function to find a way to handle the function. Basically, you need to use a replacer function to customize the "stringifying" process.

Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
  • 1
    Argh you are completely correct, I totally forgot that it changes the object, I was expecting it to just return a new serialized string. Hence I forgot about toString(), derp. – basickarl Sep 02 '19 at 13:40
  • Sometimes the brain just gets stuck on the most obvious things and you can't move on no matter what - I know that feel ;) Anyway, glad I could help. – Sebastian Kaczmarek Sep 02 '19 at 13:44