I have this http.get request but it is being triggered twice. It is within my constructor.
import { Component } from '@angular/core';
import { NavController, LoadingController } from 'ionic-angular';
import { Auth, User, IDetailedError } from '@ionic/cloud-angular';
import { Http, URLSearchParams, Headers } from "@angular/http"
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/share';
import { DetailsPage } from '../details/details';
import { LoginPage } from '../login/login';
@Component({
selector: 'page-dashboard',
templateUrl: 'dashboard.html'
})
export class Dashboard {
visitors: any;
currentSite: any;
searchDate: String = new Date().toISOString();
constructor(public navCtrl: NavController, public http: Http, public loadingCtrl: LoadingController, public user: User, public auth: Auth) {
if (this.auth.isAuthenticated()) {
this.currentSite = this.user.get('siteName',0);
let loading = this.loadingCtrl.create({
spinner: 'bubbles',
content: 'Fetching visitors ...',
duration: 5000
});
loading.present();
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Authorization', 'Basic '+ btoa(this.user.get('siteID',0)+':'+this.user.get('token',0)));
this.http.get('http://api.domain.com/visitors?search_from='+this.searchDate+'&search_to='+this.searchDate, {headers:headers}).map(res => res.json()).share().subscribe(data => {
this.visitors = data.res;
loading.dismiss();
});
} else {
this.navCtrl.setRoot(LoginPage);
}
}
Based on Angular2 http.post gets executed twice it looks like I need to include share() but when I try this I get an error ..
If I put share() at the end then I get "Property does not exist on type Subscription" and if I put it before subscribe then I get "Property share does not exist on type observable"
Can somebody tell me where share() should go ... or whether I actually have a different issue?