Currently, I am utilizing npm-mqtt to retrieve information from a different mqtt broker.
My objective is to add the obtained data to the array property of a specific class/component every time a message is received.
However, I'm facing an issue where I don't have access to the class or its properties. Instead, it seems like I'm within the scope of the mqtt client class object.
Below you can find a snippet of the code:
this.mydata: Array<any> = [];
private fetchWithMqtt(){
var client = mqtt.connect('ws://' + this.ip + ":" + Number(this.port) + "/mqtt");
// set callback handlers
client.on('close', this.onConnectionLost);
client.on('message', this.onMessageArrived);
client.on('connect', this.onConnect);
}
private onMessageArrived(topic, message) {
let tempDataset = JSON.parse(message).dataset;
this.mydata.push({ //this.mydata is undefined because this = mqtt-client
x: tempDataset[0],
y: tempDataset[1]
});
My question is: How can I push data to my class property outside of this current scope?