【发布时间】:2019-12-31 12:30:04
【问题描述】:
我在 git here 上集成 MQTT-adonis 模块 adonis-mqtt respo 在 adonis-js 应用程序中,同时服务它会引发异常
TypeError: instance[method].bind 不是函数
任何人都可以指导为什么会出现这种情况。
//event.js
'use strict'
const Event = use('Event');
const Mqtt = use('Mqtt');
// Listen to some Events of the library
Event.on('MQTT:Connected', 'Message.subscription')
Event.on('MQTT:Disconnected', 'Message.disconnected')
//Listeners/Message.js
class Message extends MqttListener {
get subscription () {
return 'Topic/Info'
}
async handleMessage (message, wildcardMatches) {
}
它似乎在这个块中抛出异常
//node_modules/adonis-mqtt/Mqtt/Mqtt.js
this.client.on('connect', this._handleConnect.bind(this)) //here at this line
this.client.on('offline', this._handleDisconnect.bind(this))
this.client.on('close', this._handleDisconnect.bind(this))
this.client.on('end', this._handleDisconnect.bind(this))
this.client.on('message', this._handleMessage.bind(this))
编辑:::
如何重现:
先决条件:
克隆这个:
移动到目录路径
npm installadonis serve --dev
更新:
从订阅函数中删除 get 时,它可以正常工作,但我无法在以下代码 sn-p 中获取所需的数据
//node_modules/adonis-mqtt/Mqtt/Mqtt.js
async _registerListener (file) {
const filePath = path.join(this.listenersPath, file)
let task
try {
task = require(filePath)
} catch (e) {
if (e instanceof ReferenceError) {
debug(
'Unable to import task class <%s>. Is it a valid javascript class?',
file)
return
} else {
throw e
}
}
// Get instance of task class
const taskInstance = ioc.make(task)
if (!taskInstance.subscription || taskInstance.subscription === '') {
console.error(`MqttListener ${file} does not have a subscription string!`)
} else {
this.client.subscribe(taskInstance.subscription) // here
debug('Subscribed to topic %s', taskInstance.subscription)
console.log('Subscribed to topic %s', taskInstance.subscription)
this.listeners.push(taskInstance)
}
}
更新:2
应用中有两个文件夹,一个是Listeners,另一个是MqttListeners,它们都有相同的命名文件Test.js,它也强制保留这两个文件。但是,Listener 文件夹中存在的文件更改不会产生任何影响。但是,从 MqttListners 文件夹的文件中删除订阅功能中的 get 前缀和 handleMessage() 中的控制台消息似乎是完美的。接收已发布的主题信息
【问题讨论】:
-
你能分享这个项目吗?所以我可以尝试重现问题
-
@CrBast 更新了重现问题的问题。
标签: javascript node.js mqtt adonis.js