Asked by:
ionic4 signalR Real Time Data Integration

Question
-
While integrating SignalR in Ionic 4, i am facing error in my code , it says "zone-evergreen.js:359 TypeError: Cannot read property 'gpsUpdated' of undefined"
The same code is running well in HMTL +CSS + Javascript, since i have to convert it into App, so i am trying with ionic. The below is TS file.
import { Component} from '@angular/core';import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';@Component({selector: 'app-home',templateUrl: 'home.page.html',styleUrls: ['home.page.scss'],})export class HomePage {customerid:string = 'C00049';hubConnection: HubConnection;axios = require('axios');apiBaseUrl = 'https://connectgps-livestream.azurewebsites.net';apiBaseUrl1 = 'https://connectgps-livestream.azurewebsites.net/api/GetGPSData/' + this.customerid;axiosConfig = {};constructor() {}ionViewWillEnter() {this.getGPSData().then(function(gpsdata) {gpsdata.forEach(this.gpsUpdated)}).then(this.getConnectionInfo).then(function(info) {let accessToken = info.accessTokenconst options = {accessTokenFactory: function() {if (accessToken) {const _accessToken = accessTokenaccessToken = nullreturn _accessToken} else {return this.getConnectionInfo().then(function(info) {return info.accessToken})}}}this.hubConnection = new HubConnectionBuilder().withUrl(info.url, options).build();this.hubConnection.on(this.customerid, this.gpsUpdated);this.hubConnection.onclose(function() {console.log('disconnected');setTimeout(function() { this.startConnection(this.hubConnection); }, 2000)})this.startConnection(this.hubConnection);}).catch(console.error)}startConnection(connection) {console.log('connecting...')connection.start({ transport: 'serverSentEvents' }).then(function() { console.log('connected!') }).catch(function(err) {console.error(err)setTimeout(function() { this.startConnection(connection) }, 2000)});}getGPSData() {return this.axios.post(`${this.apiBaseUrl1}`, null, this.axiosConfig).then(function(resp) { return resp.data }).catch(function() { return {} })}getConnectionInfo() {return this.axios.post(`${this.apiBaseUrl}/api/SignalRInfo`, null, this.axiosConfig).then(function(resp) { return resp.data }).catch(function() { return {} })}gpsUpdated(updatedGPS){console.log(updatedGPS);}}I also included script file in index.html i.e.
<script src="https://unpkg.com/@aspnet/signalr@1.0.2/dist/browser/signalr.js"></script>
Any help is deeply appreciated.
All replies
-
Hello Paramshash,
This error is not related to Cosmos DB.
Request you to post this issue to StackOverflow with ionic and nodejs tags for better visibility.
- Proposed as answer by NavtejSaini-MSFTMicrosoft employee, Moderator Tuesday, September 24, 2019 6:28 PM
-