The error http.get(...).map is not a function in [null]
occurs when you are trying to use the map
operator on the response of an HTTP GET request in Angular. This error occurs because the map
operator is not available on the response object returned by the HTTP GET method.
To fix this error, you need to modify your code to use the pipe
operator instead of the map
operator. Here is an example:
typescriptimport { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class MyService {
constructor(private http: HttpClient) {}
getData(): Observable<any> {
return this.http.get('https://jsonplaceholder.typicode.com/todos')
.pipe(
map(response => response)
);
}
}
In the above example, the map
operator is replaced with the pipe
operator, and the map
operator is passed as an argument to the pipe
method. The map
operator is used to transform the response from the HTTP GET request.
By using the pipe
operator, you can chain multiple operators to transform the response in different ways. In this example, the map
operator simply returns the response as it is.
Note that you also need to import the map
operator from the rxjs/operators
module.
Comments
Post a Comment