Cannot read property 'push' of undefined in Ionic with MQTT in if statement only
First of all I am new to Ionic and MQTT. Would be great if someone help me with this issue. I am trying to push a page in ionic CLI PRO 4.3.1. When a message arrives (1) from MQTT topic, I am trying to open a new page. The navCtrl statement is only working outside of the if statement. I am getting Cannot read property 'push' of undefined error.
My code is below;
add-device.ts
import { Component } from '@angular/core';
import { IonicPage, ModalController, NavParams, NavController, ViewController } from 'ionic-angular';
import {Paho} from 'ng2-mqtt/mqttws31';
import { SignupPage } from '../pages';
@IonicPage()
@Component({
selector: 'page-add-device',
templateUrl: 'add-device.html'
})
export class addDevicePage {
hello: any;
pushPage: any;
params: Object;
constructor(public navCtrl: NavController,
public modalCtrl: ModalController,
private viewCtrl: ViewController,
public navParams: NavParams) {}
deviceIDsend() {
console.log("MQQT");
var mqttHost = 'broker.mqttdashboard.com';
var port = 8000; // port for above
this.client = new Paho.MQTT.Client
(mqttHost, port,
"web_" + parseInt(Math.random() * 100, 10));// set callback handlers
console.log("connecting to "+ mqttHost +" "+ port);
this.client.onConnectionLost = this.onConnectionLost;
this.client.onMessageArrived = this.onMessageArrived;
//this.client.subscribe = this.subscribe;
//var options = {
// useSSL: true,
//userName: "username",
//password: "password",
//onSuccess:this.onConnect,
//onFailure:this.doFail
// }
// connect the client
this.client.connect({onSuccess:this.onConnect.bind(this)});}
// called when the client connects
onConnect(client) {
console.log("onConnect");
this.client.subscribe("/j/" +this.device_id);
this.message = new Paho.MQTT.Message(this.device_id);
this.message.destinationName = "/j/" +this.device_id;// message.qos = qos;
console.log(this.device_id);
this.client.send(this.message);}
onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
}
add-device.html
<ion-header transparent>
<ion-navbar transparent>
</ion-navbar>
</ion-header>
<ion-content class="background">
<ion-card>
<form (submit)="deviceIDsend()">
<ion-list>
<h3 text-wrap style="padding-left: 8%">{{ 'j' | translate }}</h3>
<p text-wrap style="padding-left: 8%">{{ 'j' | translate }}</p>
<div padding>
<ion-item class="item3">
<ion-label fixed>{{ 'NUMBER' | translate }}</ion-label>
<ion-input type="email" type="text" name="deviceID" [(ngModel)]="device_id"></ion-input>
</ion-item>
</div>
<div padding>
<button class="LoginButton" ion-button block value="submit"
[navPush]="pushPage">{{ 'ADD' | translate }}</button>
</div>
</ion-list>
</form>
</ion-card>
</ion-content>
add-device.module
import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { IonicPageModule } from 'ionic-angular';
import { addDevicePage } from './add-device';
@NgModule({
declarations: [
addDevicePage,
],
imports: [
IonicPageModule.forChild(addDevicePage),
TranslateModule.forChild()
],
exports: [
addDevicePage
]
})
export class addDevicePageModule { }
angular typescript ionic-framework
add a comment |
First of all I am new to Ionic and MQTT. Would be great if someone help me with this issue. I am trying to push a page in ionic CLI PRO 4.3.1. When a message arrives (1) from MQTT topic, I am trying to open a new page. The navCtrl statement is only working outside of the if statement. I am getting Cannot read property 'push' of undefined error.
My code is below;
add-device.ts
import { Component } from '@angular/core';
import { IonicPage, ModalController, NavParams, NavController, ViewController } from 'ionic-angular';
import {Paho} from 'ng2-mqtt/mqttws31';
import { SignupPage } from '../pages';
@IonicPage()
@Component({
selector: 'page-add-device',
templateUrl: 'add-device.html'
})
export class addDevicePage {
hello: any;
pushPage: any;
params: Object;
constructor(public navCtrl: NavController,
public modalCtrl: ModalController,
private viewCtrl: ViewController,
public navParams: NavParams) {}
deviceIDsend() {
console.log("MQQT");
var mqttHost = 'broker.mqttdashboard.com';
var port = 8000; // port for above
this.client = new Paho.MQTT.Client
(mqttHost, port,
"web_" + parseInt(Math.random() * 100, 10));// set callback handlers
console.log("connecting to "+ mqttHost +" "+ port);
this.client.onConnectionLost = this.onConnectionLost;
this.client.onMessageArrived = this.onMessageArrived;
//this.client.subscribe = this.subscribe;
//var options = {
// useSSL: true,
//userName: "username",
//password: "password",
//onSuccess:this.onConnect,
//onFailure:this.doFail
// }
// connect the client
this.client.connect({onSuccess:this.onConnect.bind(this)});}
// called when the client connects
onConnect(client) {
console.log("onConnect");
this.client.subscribe("/j/" +this.device_id);
this.message = new Paho.MQTT.Message(this.device_id);
this.message.destinationName = "/j/" +this.device_id;// message.qos = qos;
console.log(this.device_id);
this.client.send(this.message);}
onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
}
add-device.html
<ion-header transparent>
<ion-navbar transparent>
</ion-navbar>
</ion-header>
<ion-content class="background">
<ion-card>
<form (submit)="deviceIDsend()">
<ion-list>
<h3 text-wrap style="padding-left: 8%">{{ 'j' | translate }}</h3>
<p text-wrap style="padding-left: 8%">{{ 'j' | translate }}</p>
<div padding>
<ion-item class="item3">
<ion-label fixed>{{ 'NUMBER' | translate }}</ion-label>
<ion-input type="email" type="text" name="deviceID" [(ngModel)]="device_id"></ion-input>
</ion-item>
</div>
<div padding>
<button class="LoginButton" ion-button block value="submit"
[navPush]="pushPage">{{ 'ADD' | translate }}</button>
</div>
</ion-list>
</form>
</ion-card>
</ion-content>
add-device.module
import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { IonicPageModule } from 'ionic-angular';
import { addDevicePage } from './add-device';
@NgModule({
declarations: [
addDevicePage,
],
imports: [
IonicPageModule.forChild(addDevicePage),
TranslateModule.forChild()
],
exports: [
addDevicePage
]
})
export class addDevicePageModule { }
angular typescript ionic-framework
show your whole component code, so we can figure out what's wrong with yourthis.navCtrl
property
– Artyom Amiryan
Nov 22 '18 at 20:51
Do you have navCtrl passed in constructor?
– Aragorn
Nov 22 '18 at 21:00
Hello @Aragorn, I had included as public navCtrl: NavController. navCtrl is working outside of the if statement.
– Yildiz
Nov 22 '18 at 21:05
@ArtyomAmiryan I included whole component.
– Yildiz
Nov 22 '18 at 21:15
add a comment |
First of all I am new to Ionic and MQTT. Would be great if someone help me with this issue. I am trying to push a page in ionic CLI PRO 4.3.1. When a message arrives (1) from MQTT topic, I am trying to open a new page. The navCtrl statement is only working outside of the if statement. I am getting Cannot read property 'push' of undefined error.
My code is below;
add-device.ts
import { Component } from '@angular/core';
import { IonicPage, ModalController, NavParams, NavController, ViewController } from 'ionic-angular';
import {Paho} from 'ng2-mqtt/mqttws31';
import { SignupPage } from '../pages';
@IonicPage()
@Component({
selector: 'page-add-device',
templateUrl: 'add-device.html'
})
export class addDevicePage {
hello: any;
pushPage: any;
params: Object;
constructor(public navCtrl: NavController,
public modalCtrl: ModalController,
private viewCtrl: ViewController,
public navParams: NavParams) {}
deviceIDsend() {
console.log("MQQT");
var mqttHost = 'broker.mqttdashboard.com';
var port = 8000; // port for above
this.client = new Paho.MQTT.Client
(mqttHost, port,
"web_" + parseInt(Math.random() * 100, 10));// set callback handlers
console.log("connecting to "+ mqttHost +" "+ port);
this.client.onConnectionLost = this.onConnectionLost;
this.client.onMessageArrived = this.onMessageArrived;
//this.client.subscribe = this.subscribe;
//var options = {
// useSSL: true,
//userName: "username",
//password: "password",
//onSuccess:this.onConnect,
//onFailure:this.doFail
// }
// connect the client
this.client.connect({onSuccess:this.onConnect.bind(this)});}
// called when the client connects
onConnect(client) {
console.log("onConnect");
this.client.subscribe("/j/" +this.device_id);
this.message = new Paho.MQTT.Message(this.device_id);
this.message.destinationName = "/j/" +this.device_id;// message.qos = qos;
console.log(this.device_id);
this.client.send(this.message);}
onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
}
add-device.html
<ion-header transparent>
<ion-navbar transparent>
</ion-navbar>
</ion-header>
<ion-content class="background">
<ion-card>
<form (submit)="deviceIDsend()">
<ion-list>
<h3 text-wrap style="padding-left: 8%">{{ 'j' | translate }}</h3>
<p text-wrap style="padding-left: 8%">{{ 'j' | translate }}</p>
<div padding>
<ion-item class="item3">
<ion-label fixed>{{ 'NUMBER' | translate }}</ion-label>
<ion-input type="email" type="text" name="deviceID" [(ngModel)]="device_id"></ion-input>
</ion-item>
</div>
<div padding>
<button class="LoginButton" ion-button block value="submit"
[navPush]="pushPage">{{ 'ADD' | translate }}</button>
</div>
</ion-list>
</form>
</ion-card>
</ion-content>
add-device.module
import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { IonicPageModule } from 'ionic-angular';
import { addDevicePage } from './add-device';
@NgModule({
declarations: [
addDevicePage,
],
imports: [
IonicPageModule.forChild(addDevicePage),
TranslateModule.forChild()
],
exports: [
addDevicePage
]
})
export class addDevicePageModule { }
angular typescript ionic-framework
First of all I am new to Ionic and MQTT. Would be great if someone help me with this issue. I am trying to push a page in ionic CLI PRO 4.3.1. When a message arrives (1) from MQTT topic, I am trying to open a new page. The navCtrl statement is only working outside of the if statement. I am getting Cannot read property 'push' of undefined error.
My code is below;
add-device.ts
import { Component } from '@angular/core';
import { IonicPage, ModalController, NavParams, NavController, ViewController } from 'ionic-angular';
import {Paho} from 'ng2-mqtt/mqttws31';
import { SignupPage } from '../pages';
@IonicPage()
@Component({
selector: 'page-add-device',
templateUrl: 'add-device.html'
})
export class addDevicePage {
hello: any;
pushPage: any;
params: Object;
constructor(public navCtrl: NavController,
public modalCtrl: ModalController,
private viewCtrl: ViewController,
public navParams: NavParams) {}
deviceIDsend() {
console.log("MQQT");
var mqttHost = 'broker.mqttdashboard.com';
var port = 8000; // port for above
this.client = new Paho.MQTT.Client
(mqttHost, port,
"web_" + parseInt(Math.random() * 100, 10));// set callback handlers
console.log("connecting to "+ mqttHost +" "+ port);
this.client.onConnectionLost = this.onConnectionLost;
this.client.onMessageArrived = this.onMessageArrived;
//this.client.subscribe = this.subscribe;
//var options = {
// useSSL: true,
//userName: "username",
//password: "password",
//onSuccess:this.onConnect,
//onFailure:this.doFail
// }
// connect the client
this.client.connect({onSuccess:this.onConnect.bind(this)});}
// called when the client connects
onConnect(client) {
console.log("onConnect");
this.client.subscribe("/j/" +this.device_id);
this.message = new Paho.MQTT.Message(this.device_id);
this.message.destinationName = "/j/" +this.device_id;// message.qos = qos;
console.log(this.device_id);
this.client.send(this.message);}
onMessageArrived(message) {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
}
}
add-device.html
<ion-header transparent>
<ion-navbar transparent>
</ion-navbar>
</ion-header>
<ion-content class="background">
<ion-card>
<form (submit)="deviceIDsend()">
<ion-list>
<h3 text-wrap style="padding-left: 8%">{{ 'j' | translate }}</h3>
<p text-wrap style="padding-left: 8%">{{ 'j' | translate }}</p>
<div padding>
<ion-item class="item3">
<ion-label fixed>{{ 'NUMBER' | translate }}</ion-label>
<ion-input type="email" type="text" name="deviceID" [(ngModel)]="device_id"></ion-input>
</ion-item>
</div>
<div padding>
<button class="LoginButton" ion-button block value="submit"
[navPush]="pushPage">{{ 'ADD' | translate }}</button>
</div>
</ion-list>
</form>
</ion-card>
</ion-content>
add-device.module
import { NgModule } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { IonicPageModule } from 'ionic-angular';
import { addDevicePage } from './add-device';
@NgModule({
declarations: [
addDevicePage,
],
imports: [
IonicPageModule.forChild(addDevicePage),
TranslateModule.forChild()
],
exports: [
addDevicePage
]
})
export class addDevicePageModule { }
angular typescript ionic-framework
angular typescript ionic-framework
edited Nov 22 '18 at 21:08
georgeawg
34.1k115370
34.1k115370
asked Nov 22 '18 at 20:50
YildizYildiz
12
12
show your whole component code, so we can figure out what's wrong with yourthis.navCtrl
property
– Artyom Amiryan
Nov 22 '18 at 20:51
Do you have navCtrl passed in constructor?
– Aragorn
Nov 22 '18 at 21:00
Hello @Aragorn, I had included as public navCtrl: NavController. navCtrl is working outside of the if statement.
– Yildiz
Nov 22 '18 at 21:05
@ArtyomAmiryan I included whole component.
– Yildiz
Nov 22 '18 at 21:15
add a comment |
show your whole component code, so we can figure out what's wrong with yourthis.navCtrl
property
– Artyom Amiryan
Nov 22 '18 at 20:51
Do you have navCtrl passed in constructor?
– Aragorn
Nov 22 '18 at 21:00
Hello @Aragorn, I had included as public navCtrl: NavController. navCtrl is working outside of the if statement.
– Yildiz
Nov 22 '18 at 21:05
@ArtyomAmiryan I included whole component.
– Yildiz
Nov 22 '18 at 21:15
show your whole component code, so we can figure out what's wrong with your
this.navCtrl
property– Artyom Amiryan
Nov 22 '18 at 20:51
show your whole component code, so we can figure out what's wrong with your
this.navCtrl
property– Artyom Amiryan
Nov 22 '18 at 20:51
Do you have navCtrl passed in constructor?
– Aragorn
Nov 22 '18 at 21:00
Do you have navCtrl passed in constructor?
– Aragorn
Nov 22 '18 at 21:00
Hello @Aragorn, I had included as public navCtrl: NavController. navCtrl is working outside of the if statement.
– Yildiz
Nov 22 '18 at 21:05
Hello @Aragorn, I had included as public navCtrl: NavController. navCtrl is working outside of the if statement.
– Yildiz
Nov 22 '18 at 21:05
@ArtyomAmiryan I included whole component.
– Yildiz
Nov 22 '18 at 21:15
@ArtyomAmiryan I included whole component.
– Yildiz
Nov 22 '18 at 21:15
add a comment |
1 Answer
1
active
oldest
votes
Can you try the arrow function?
My only suspect is this
, but I'm not 100% sure as OP says this.navCtrl.push
works outside of the if
block.
onMessageArrived = (message) => {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
1
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437834%2fcannot-read-property-push-of-undefined-in-ionic-with-mqtt-in-if-statement-only%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Can you try the arrow function?
My only suspect is this
, but I'm not 100% sure as OP says this.navCtrl.push
works outside of the if
block.
onMessageArrived = (message) => {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
1
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
add a comment |
Can you try the arrow function?
My only suspect is this
, but I'm not 100% sure as OP says this.navCtrl.push
works outside of the if
block.
onMessageArrived = (message) => {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
1
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
add a comment |
Can you try the arrow function?
My only suspect is this
, but I'm not 100% sure as OP says this.navCtrl.push
works outside of the if
block.
onMessageArrived = (message) => {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
Can you try the arrow function?
My only suspect is this
, but I'm not 100% sure as OP says this.navCtrl.push
works outside of the if
block.
onMessageArrived = (message) => {
console.log("onMessageArrived:" + message.payloadString);
if (message.payloadString == "1") {
console.log('in');
this.navCtrl.push('SignupPage');
}
else{
console.log('hello');
}
}
answered Nov 22 '18 at 21:29
AragornAragorn
2,56911430
2,56911430
1
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
add a comment |
1
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
1
1
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
It works very well. Thank you for your help!
– Yildiz
Nov 22 '18 at 21:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437834%2fcannot-read-property-push-of-undefined-in-ionic-with-mqtt-in-if-statement-only%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
show your whole component code, so we can figure out what's wrong with your
this.navCtrl
property– Artyom Amiryan
Nov 22 '18 at 20:51
Do you have navCtrl passed in constructor?
– Aragorn
Nov 22 '18 at 21:00
Hello @Aragorn, I had included as public navCtrl: NavController. navCtrl is working outside of the if statement.
– Yildiz
Nov 22 '18 at 21:05
@ArtyomAmiryan I included whole component.
– Yildiz
Nov 22 '18 at 21:15