0

In Angular, I have a http service that I need to return a promise from because the library I am using relies on a promise based http client. However, the error ERROR Error: Uncaught (in promise): TypeError: this is undefined occurs in my code whenever this http service is called.

For example the http service is being called in this other service by a simple:

const destinyManifest = getDestinyManifest(this.http.$fetch)

and the http client code looks like:

export class APIHTTPService {
    constructor(private http: HttpClient) {}

    $fetch(config: HttpClientConfig) {
        const httpOptions = {
            headers: new HttpHeaders({
                'X-API-KEY': environment.bungieAPIKey,
            }),
            params: new HttpParams(config.params),
        }
        let promise = new Promise<any>((resolve, reject) => {
            this.http
                .get(config.url, httpOptions)
                .toPromise()
                .then((response) => {
                    resolve(response)
                    console.log('response')
                })
        })
        return promise
    }
}
Zeta
  • 1
  • `getDestinyManifest(this.http.$fetch)` -> `getDestinyManifest((config: HttpClientConfig) => this.http.$fetch(config))` – VLAZ Dec 04 '21 at 08:09
  • @VLAZ Yes, it kinda teed me off to the fact that the error wasn't necessarily coming from this function, but how it was utilized elsewhere due to the context surrounding it's usage. Thank you for the hint! – Zeta Dec 04 '21 at 08:12

0 Answers0