0

I have spent hours trying different ways to get this Observable to work in ionic / angular. I have tried different setups and interfaces to plugin to Observable which has properties which are strings. I have tried plugging in JSON as an interface. I simply need to do a GET to an https server with extra parameters. I keep getting errors stating that essentially the returned http cannot properly be typed to the observable defined in getGP. error is enter image description here When removing the subscribe I get an almost identical error when JSON isnt used as an interface. A simple GET/POST is easy to do in vanilla and angular is driving me nuts. I also need to save the response in a variable and inject the data into another module. I have looked through the rxjs docs and just at a loss. Help would be appreciated. I am running the latest angular and the latest ionic framework.

import { HttpClient, HttpHeaders } from '@angular/common/http';

getGP(): Observable<JSON> {
    this.httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'token': 'token',
            'mtype': 'g',
        })
    }

    return this.http.get<JSON>(this.base_path + '/pathtoserver', this.httpOptions).subscribe(res => console.log(res))

}
sasy solutions
  • 179
  • 1
  • 9
  • You don't want to use JSON as a type, you should define what the object returned should be as an Interface, give it a name and use that name instead of JSON. Second, you do not want to subscribe in the service, remove the subscribe. – dmcgrandle Aug 25 '19 at 03:08

1 Answers1

0

You specific your function to return a Observable but you returned a Subscription instead. also change JSON to any see if it works

Removed the subscription it should compile.

getGP(): Observable<any> {
    this.httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'token': 'token',
            'mtype': 'g',
        })
    }

    return this.http.get<JSON>(this.base_path + '/pathtoserver', this.httpOptions)

}
Fan Cheung
  • 10,745
  • 3
  • 17
  • 39