【问题标题】:Sending function as argument results in undefined将函数作为参数发送会导致未定义
【发布时间】:2019-09-02 13:33:04
【问题描述】:
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

【问题讨论】:

    标签: typescript


    【解决方案1】:

    因为JSON.stringifyomits the functions by default

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

    【讨论】:

    • Argh 你是完全正确的,我完全忘记了它改变了对象,我期待它只是返回一个新的序列化字符串。因此我忘记了 toString(), derp.
    • 有时大脑会卡在最明显的事情上,无论如何你都无法继续前进 - 我知道那种感觉;)无论如何,很高兴我能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    相关资源
    最近更新 更多