In order to check if internet and id connection are available, I need to make a server request. I have implemented the Ionic Native Network Plugin following their official documentation.
Here is my code snippet:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { Auth } from '../../providers/auth/auth';
import { Network } from '@ionic-native/network';
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
constructor(private network: Network,private alertCtrl: AlertController,public navCtrl: NavController, public navParams: NavParams,public authService: Auth, public loadingCtrl: LoadingController) {
}
ionViewDidLoad() {
// watch network for a disconnect
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
//Check if already authenticated
this.authService.checkAuthentication().then((res) => {
console.log("Already authorized");
this.loading.dismiss();
this.navCtrl.setRoot(HomePage);
}, (err) => {
console.log("Not already authorized");
this.loading.dismiss();
});
});
// stop connect watch
connectSubscription.unsubscribe();
console.log('ionViewDidLoad LoginPage');
}
}
Upon checking the console, I encountered the following error:
https://i.sstatic.net/fie41.png
Why does this occur? Should I include 'Network' in the providers array as well?
UPDATE
Following Daniel B's suggestion, I added 'Network' to the providers array which resolved the previous error. However, now it doesn't enter the this.network.onConnect()
function.