Typescript 将函数作为参数发送会导致未定义
在我的代码中的其他地方:Typescript 将函数作为参数发送会导致未定义,typescript,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 {
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}`);
为什么回调
未定义?因为JSON.stringify
请参阅以找到处理该函数的方法。基本上,您需要使用替换函数来自定义“字符串化”过程。Argh您完全正确,我完全忘记它更改了对象,我希望它只返回一个新的序列化字符串。因此我忘记了toString(),derp。有时候,大脑会被最明显的事情卡住,不管发生什么,你都无法前进——我知道那种感觉;)不管怎样,很高兴我能帮上忙。
2019-09-02T13:29:59.370Z debug: Callback to add: undefined