MQTT.js subscribe() 函数的回调未捕获错误消息

Callback of MQTT.js subscribe() function not capturing error message

我正在使用 MQTT.js 订阅实时遥测数据。

client.on('connect', function () {  
    console.log('client connected');
    client.subscribe('flespi/state/gw/devices/' + flespi_id + '/telemetry/position', 
      { qos: 0 }, 
      function (err, granted){
        console.log(err.toString());
      }
   );      
}
=>client connected
=>Uncaught TypeError: err is null

有时订阅不成功,所以我使用docs中指示的回调函数来检索错误。

我故意给了一个错误的订阅令牌,所以即使它连接了,也无法订阅。问题是它说没有错误 (err is null)。那我怎么知道错误是什么?

这里有一个类似的问题,但是 publish() 而不是 subscribe()

阅读 HiveMQ 上的 MQTT js docs carefully and read something about the actual MQTT messages too - ideally the standard itself or the nice interpretationerr 回调参数用于全局故障,如传输层错误。如果客户端发送格式错误的主题名称,或者客户端没有足够的权限(这正是 HiveMQ 提到的),您将在 granted 数组中发现您的失败代码为 0x80。

如果特定代理上尚不存在特定主题,则这不是 MQTT 中的错误。您可以订阅通配符,经纪人应该如何处理?仅当向其发布第一条消息时,主题才可能开始存在。