0

In the below posted code I have a method that returns a subscriber. What I want to do is to get or retrieve that data from the subscriber. In other words, I want to do something as follows:

console.log(getRouteGeometryFromAlexandriaToAmsterdam())

and that log statement must display the contents of

data["features"][0]['geometry']

When I just use the log statement stated above I receive a subscriber object. please let me know how to get the data from a subscriber object.

code:

public getRouteGeometryFromAlexandriaToAmsterdam() {
    return this.httpClient.get("https://api.openrouteservice.org/v2/directions/.....")
        .subscribe((data)=> {
            console.log("getRouteGeometryFromAlexandriaToAmsterdam = " , data["features"][0]['geometry']);
            return (data["features"][0]['geometry']);
        });
}
ikos23
  • 4,879
  • 10
  • 41
  • 60
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • You cannot return from the subscription callback. You need to return the observable and subscribe where it's response is required. See this canonical answer: https://stackoverflow.com/a/14220323/6513921 – ruth Apr 19 '21 at 06:40

3 Answers3

1

It is always NOT advisable to return any value inside a subscribe callback - reason is because Observables is asynchronous, but your code is synchronous.

You did correct in your method actually, you just need to subscribe in your component (since you tag angular) and not your service. Something like this:

public getRouteGeometryFromAlexandriaToAmsterdam() {
    return this.httpClient.get("https://api.openrouteservice.org/v2/directions/.....")

And in your component, do this:

this.getRouteGeometryFromAlexandriaToAmsterdam()
    .subscribe(data=>{
        console.log("getRouteGeometryFromAlexandriaToAmsterdam = " , data["features"][0]['geometry']);
    })
CozyAzure
  • 8,280
  • 7
  • 34
  • 52
0

You can handle the response with some other function like this:

public getRouteGeometryFromAlexandriaToAmsterdam() {
    return this.httpClient.get("https://api.openrouteservice.org/v2/directions/.....")
        .subscribe((data)=> {
            this.handleResponse(data["features"][0]["geometry"]);
        });
}

public handleResponse(res) {
    return res;
}

console.log(getRouteGeometryFromAlexandriaToAmsterdam());
Vinay Somawat
  • 639
  • 14
  • 23
0

You are returning an observable in getRouteGeometryFromAlexandriaToAmsterdam method which means you should subscribe it to get it's value.

You can return data instead of http service:

public getRouteGeometryFromAlexandriaToAmsterdam() {
    this.httpClient.get("https://api.openrouteservice.org/v2/directions/.....")
        .subscribe((data)=> {
            console.log(data);
            return (data);
        });
}

Or add another method:

public getRouteGeometryFromAlexandriaToAmsterdam() {
    return this.httpClient.get("https://api.openrouteservice.org/v2/directions/.....")
}

  getData(){
   this.getRouteGeometryFromAlexandriaToAmsterdam().subscribe(res => {
    console.log(res)})
  }
Saghi Shiri
  • 537
  • 5
  • 19