0

I am making a request using observable. and trying to subcribe the value. But getting error on typescript. any on help me?

I like to do this:

public getCountry(lat,lan):Observable<any>{
    return this.http.get(this.googleApi+lat+','+lan+'&sensor=false').subscribe(data => {
        return this.genertedData(data);
    } );
}

But getting error as follows:

Observable Error

UPDATES

 public getCountry(lat,lan):Observable<any>{

        return this.http.get(this.googleApi+lat+','+lan+'&sensor=false').map( data => {

            data.results.map( array => {
                let details = array.address_components.find( obj => obj.types.includes("country") );
                this.countryDetails.countryLongName = details.long_name;
                this.countryDetails.countryShortName = details.short_name;
            })

            return this.countryDetails;
        })

    }
user2024080
  • 1
  • 14
  • 56
  • 96

2 Answers2

2

The problem is that your return type states Observable<any>, where as you actually return whatever this.genertedData(data) returns (Hint: Sounds like a typo in your function. Guess it should be called generatedData ?).

Best practice would be to move the http call into a service and subscribe to its returned Observable within your component.

So to speak:

// => service.ts
public getCountryObservable(lat,lan):Observable<any> {
    return this.http.get(this.googleApi+lat+','+lan+'&sensor=false');
}

Your component would look something like:

// => component.ts
export class YourComponent {
  constructor(public yourService: YourService) {}
  getCountry(lat, lan): whateverDataTypeYourGeneratedDataReturns {
    this.yourService.getCountryObservable(lat, lan).subscribe((data) => {
      this.data = this.generatedData(data);
    });
  }
}
Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
  • `getCountryObservable` how to map the details? I require to add the filter – user2024080 Apr 28 '18 at 15:30
  • Please refrain from asking multiple questions inside a single post. Also, it totally depends on what your actual data returned by the server looks like. I suggest you create a new question with more details. It sounds like you're trying to accomplish something fairly basic. – Philipp Meissner Apr 28 '18 at 15:33
  • the problem is the object which comes from backend is unknow. but getting the declaration issue as mentioned. that's what i added the question. please see my edited update. I am getting underline at `data.results` how can i handle this? – user2024080 Apr 28 '18 at 15:56
  • `Property 'results' does not exist on type 'Object'` - this is what the error – user2024080 Apr 28 '18 at 16:04
  • If you *really* do not know what the object you receive from the backend looks like (which sounds like you got worse problems, really) you may use the `any` type instead of `Object` (as my answer already states). Better option would be to follow this: https://stackoverflow.com/questions/36607979/how-to-get-around-property-does-not-exist-on-object – Philipp Meissner Apr 28 '18 at 16:11
  • agree. But finding dificult to integrate. can you update your answer? ( please don't mind ) – user2024080 Apr 28 '18 at 16:14
  • Again, the answer is "up to date" already. Simply change your return type from `Observable` to `Observable` for your `getCountry` method. – Philipp Meissner Apr 28 '18 at 16:25
  • still getting the same issue after updating like `public getCountry(lat,lan):Observable{` - help me – user2024080 Apr 28 '18 at 16:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169997/discussion-between-philipp-meissner-and-user2024080). – Philipp Meissner Apr 28 '18 at 16:40
1

Since the return type of the function is Observable<any>, I guess it should just return this.http.get(this.googleApi+lat+','+lan+'&sensor=false')

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
Joseph Wu
  • 4,786
  • 1
  • 21
  • 19
  • updating this line `return this.http.get(this.googleApi+lat+','+lan+'&sensor=false').map((data:any) =>` helped me. thanks for your time!! – user2024080 Apr 29 '18 at 03:25