3
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class GeoService {
    constructor(private http: Http) { }

    private extractData(res : any){
        if(res.status < 200 || res.status >=300){
            throw new Error('Bad response sttus:' + res.status);
        }
        this.serviceData = (res.json());
        return this.serviceData || {};
    }

    loaddata(term: string): Observable<any> {
     return this.http.get('http://maps.google.com/maps/api/geocode/json?address=' + term + 'CA&sensor=false')
      .map(this.extractData);
  }
}

Why does it say 'Property 'serviceData' does not exist on type GeoService'?

danday74
  • 52,471
  • 49
  • 232
  • 283
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

2 Answers2

3

Well it doesn't, when you in your extractData function try to refer to this.serviceData, you have not declared it anywhere in your service.

Try:

private extractData(res : any){
    if(res.status < 200 || res.status >=300){
        throw new Error('Bad response sttus:' + res.status);
    }
    let serviceData = res.json(); // declare here!
    return serviceData || {};
}

Other option is that you in your service actually declare the serviceData, then you can use this in your function.

export class GeoService {
   private serviceData;
   ....

// and then use  this:
private extractData(res : any){
    if(res.status < 200 || res.status >=300){
        throw new Error('Bad response sttus:' + res.status);
    }
    this.serviceData = (res.json());
    return this.serviceData || {};
}

}

Then you can use these in the component, that is calling the function in the service like so in your OnInit method or in your constructor:

data; // declare a variable where you want to store the data

constructor(private geoService: GeoService)  {
   this.geoService.loaddata() 
     .subscribe(data => {
       this.data = data;
       console.log(this.data); // your values here!
     });
}

I would suggest you do this in the OnInit method though.

AT82
  • 71,416
  • 24
  • 140
  • 167
1
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class GeoService {
    public serviceData:any;
    constructor(private http: Http) { }

    private extractData(res : any){
        if(res.status < 200 || res.status >=300){
            throw new Error('Bad response sttus:' + res.status);
        }
        this.serviceData = (res.json());
        return this.serviceData || {};
    }

    loaddata(term: string): Observable<any> {
     return this.http.get('http://maps.google.com/maps/api/geocode/json?address=' + term + 'CA&sensor=false')
      .map(this.extractData);
  }
}
anshuVersatile
  • 2,030
  • 1
  • 11
  • 18