I'm encountering a typescript error as I migrate a Paho MQTT function from Angular 1 to Angular 2 - what could be causing this issue?

When connecting to my MQTT broker, I am working on several tasks. In my Ionic 2 and Angular 2 application, I have created an MQTT provider. Here is the provider code:

import { Component } from '@angular/core';
import { NavController, ViewController } from 'ionic-angular';
import { Observable } from 'rxjs/Observable';
import { Paho } from 'ng2-mqtt/mqttws31';

@Component({
  selector: 'page-greywater',
  templateUrl: 'greywater.html'
})
export class MQTT_Greenchain {
  private _client: Paho.MQTT.Client;
  private options = {
    userName: 'rdjghvoh',
    password: 'w7Ex0VTqZViw',
    timeout: 30,
    useSSL:true,
    onSuccess:this.onConnected,
  };
  private topic: string;
  public displayedMessage: string;
  public mes: Paho.MQTT.Message;
  public constructor() {
    this._client = new Paho.MQTT.Client(
      "m20.cloudmqtt.com",
      Number(30775),
      "",
      "peter"
    );
    this._client.onConnectionLost = (responseObject: {errorCode: Number, errorMessage: string}) => {
      console.log('poes');
      console.log(responseObject.errorMessage);
    };

    this._client.onMessageArrived = (message: Paho.MQTT.Message) => {
      this.onMessageArr(message);
      console.log('Message arrived.');
    };
    this.topic = "haha";
    this.displayedMessage = "what I was";
  }
  connectMe() {
    console.log("MQTT OPTIONS: " + this.options);
    this._client.connect(this.options);
  }
  private onConnected(): void {
    console.log('Connected to broker.');
    this._client.subscribe(this.topic);
    this.mes = new Paho.MQTT.Message("-1"); // -1 => Notify
    this.mes.destinationName = this.topic;
    this._client.send(this.mes);
  }
  private onMessageArr(message: Paho.MQTT.Message){
    this.displayedMessage = message.payloadString;
  }
}

In Angular 1, I had no issues calling the following function and getting everything related to MQTT working. The Angular 1 function looks like this:

function onConnect() {
  sharedUtils.hideLoading();
  console.log("onConnect, CURRENT TOPIC: " + mqttData.currentTopic);
  client.subscribe(mqttData.currentTopic);
}

The argument in the above function, mqttData.currentTopic, is simply a string.

This function only accepts 1 argument even though it can technically accept 2 (an options object).

In Angular 2, TypeScript throws an error when I try to call the function with just one argument:

Supplied parameters do not match any signature of call target

Why does Angular 2 not allow me to call the function with only one argument as in Angular 1? When I provide {} as a second argument:

this._client.subscribe(this.topic, {});

I get the error:

AMQJS0005E Internal error. Error Message: Cannot read property 'subscribe' of undefined, Stack trace: TypeError: Cannot read property 'subscribe' of undefined

This error is received in the response object parameter passed to the onConnectionLost callback function.

Even though 'this._client' seems defined based on the console message 'Connected to broker.', where the onSuccess callback is evidently called, why am I encountering these issues?

What am I missing here?

Answer №1

Give this a shot and let me know if everything is running smoothly

this.client.subscribe(this.topic, '');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improving the retrieval of API data using personalized React hooks when searching by modifying keywords

I'm just starting out with React Hooks and recently wrote a small code snippet that displays a list of courses to users. This code includes two main components, CourseList and Course, as well as a custom hook called useCourseList. Here's the code ...

Unable to locate "Gruntfile.js" Node module for task execution

I am currently in the process of developing a module that enables node to execute Grunt tasks via the command line. This Node module is globally installed at : C:\Users\pcharpin\AppData\Roaming\npm\node_modules\task-app ...

Optimal approach for managing numerous modals containing map data in Next.js

Hey there, I'm facing an issue while trying to utilize map data in conjunction with modals. Although I have set the state for all modals, when I use an array object within the map data, the modals end up showing duplicated. To provide clarity, let me ...

Is it feasible to integrate gMaps with Angular2? How can I go about accomplishing this task?

In developing a typeahead service for cities and similar functions, I require the use of a typeahead directive (ng2-bootstrap) with an array of strings provided by the service. Google Maps is my chosen solution for this task. Below is code extracted from ...

When you're downloading a file in Safari, the filename ends up being displayed as 'unidentified'

I am encountering an issue with the code I implemented that downloads a file in Safari, but the filename appears as 'untitled'. Interestingly, it works fine in other browsers. var saveData = (function () { var base64 = "data:application/mswo ...

Is there a way to update a variable in a controller using AJAX?

Is it possible to update a variable in the controller using Ajax? Controller: $basl = array(2018,11,18,0,0); $deger = 3; $baslamatarihi=Carbon::create($basl[0],$basl[1],$basl[2],$basl[3],$basl[4]); $bitistarihi = Carbon::create($basl[0],$basl[1],$basl[2] ...

Setting properties on functions and defining their prototype

My task involves working on the following block of code: function Vector(x, y) { this.x = x || 0; this.y = y || 0; } Vector.add = function(a, b) { return new Vector(a.x + b.x, a.y + b.y); }; Vector.sub = function(a, b) { return new Vecto ...

The unzipper will disregard any empty directories within the file

I am a Japanese Web Developer. Unfortunately, I struggle with English. https://www.npmjs.com/package/unzipper This library is currently in use by me. Below you will find my code snippet: // unzip module import fs from 'fs-extra' import unzip ...

JavaScript: Locate web addresses within a block of text and convert them into clickable hyper

How can we convert the following PHP code to JavaScript in order to replace URL links inside text blobs with HTML links? A jsfiddle has been initiated. <?php // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a- ...

What is causing my Javascript media query for the sidebar to not function properly?

I am working on implementing a sidebar that slides out 250px using JavaScript on the desktop view. However, I want the sidebar to take up 100% width when viewed on mobile devices. Despite my attempts to use Media Queries in JavaScript to achieve this, I am ...

Customized slider in jQuery UI allowing users to select height using a scaled ruler

Currently, I am tackling a challenging math problem related to adjusting the height selector input. Essentially, I have implemented a jQuery UI slider for choosing a height. It operates in inches and ranges from 0 to 120 (equivalent to 10 feet in height). ...

Encountering challenges when trying to incorporate error-handling functionality into Highcharts

I've been attempting to incorporate custom error handling in Highcharts by utilizing the Highcharts.error function within my Angular 7 application, but it's resulting in an error. Highcharts.error = function (code: string): void { }; Error T ...

Steps to include all dependencies in an angular application

Managing a large Angular application can be challenging. I currently use npm install to install all packages and manually load them in my index.html file, like this: <script src="node_modules/angular/angular.js"></script> Similarly, I load ot ...

What are the steps to view my HTML webpage on a smartphone?

Recently, I successfully created an HTML webpage with CSS and JS that looks and functions perfectly on my PC. However, when attempting to access it on my phone, I encountered some issues. Despite transferring all the necessary files to my phone, only the ...

The MUI5 drawer overlapping a modal dialog

Is it possible to use MUI5 to open a side drawer on top of a modal dialog that triggers it? Currently, the dialog triggers the side drawer but it opens behind this. It appears as though a modal dialog (drawer) is attempting to open over an already-opened ...

Is Javascript necessary for submitting a form to a PHP script?

In my current project, I am working on a page to edit information in a database. While I know how to create the form in HTML and the PHP script that runs on the server, I am facing a challenge with JavaScript. My understanding of JavaScript is limited, and ...

Just starting out with json/ajax and I received an error in the Console that says: Uncaught TypeError: Cannot read property 'length' of undefined

I’m encountering an issue with my code. I must emphasize that I’m brand new to this, so please bear with me if the solution is simple. I did attempt to solve it myself before reaching out for help and searched through various posts with similar errors, ...

Receiving information from a promise within an if statement in Vue

Recently, I encountered an issue with my login function. I needed to store some data in my session for future use but faced a problem where the data was being pushed into the session before I could retrieve the result from a promise. Here's the snipp ...

Error in sending Ajax form

I have a form that is set up to send data to a server. When the form is in its regular HTML format, everything works smoothly and all data is successfully transmitted to the server without any issues. However, as soon as I switch the form to use AJAX for s ...

Transform a list separated by commas into an unordered list

Seeking a PHP, Jquery, or JavaScript method to convert comma-separated data into an unordered list. For clarification, I have uploaded a CSV file to WordPress where one section of content is separated by commas and I am looking to display it as a list. A ...