Home > Enterprise >  Response.Json() is not a function
Response.Json() is not a function

Time:03-20

I'm following an rxjs that shows how to use rxjs with http calls in angular. But when i'm,trying to use response.json in the mapping function, i've an error in the console saying that: ERROR TypeError: response.json is not a function. this is the code of my service:

 mappingFn() {
return this.http
  .get('https://opentdb.com/api.php?amount=10')
  .pipe(map((response: Response) => response.json()))};

this is the code of the components:

 this.svc.mappingFn().subscribe({
  next: (res) => {
    console.log('mapping:', res);
  },
});

I looked on different blogs, but i couldn't find something usefull. Please help me. Thank you

CodePudding user response:

This can be influenced by your response. Some of them does not work directly. Go to the network tab and watch your data model. On the other side, make sure that you actually have a response, and response isn't undefined.

CodePudding user response:

You don't have map the result to json, that was happen automatically by HttpClient.get(). Remove the map and write mappingFn as.

mappingFn() {
return this.http
  .get('https://opentdb.com/api.php?amount=10')};
  • Related