将函数作为参数发送导致未定义

时间:2019-09-02 13:33:04

标签: typescript

broker.onTopic = function onTopic(topic: string, callback: Function): void {
    log.debug(`Callback to add: ${JSON.stringify(callback, null, 4)}`);
    //...more 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}`);

日志显示:

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

为什么要回调undefined

1 个答案:

答案 0 :(得分:0)

因为JSON.stringify omits the functions by default

请参见JSON.stringify function,以找到处理函数的方法。基本上,您需要使用替换器功能来自定义“字符串化”过程。