My map function in this code block:
public async getAllWidgets2(): Promise<Widget[]> {
let url = "myUrl";
var items = [];
return axios.get(url).then(result => {
console.log("results" + result)
let result2: Widget[] = [];
result.map((item) => { result2.push(this.parseWidget(item)); });
let data: Widget[] = result2;
return(data);
} ) }
The map functions above gives me an error "Property 'map' does not exist on type 'AxiosResponse'.ts(2339)"
I searched on answers on StackOverflow and tried:
import { map } from 'rxjs/operators';
But still gives me the same error. Any way to solve it?
The error "Property 'map' does not exist on type" occurs when we call the map() method on a value that isn't an array. To solve the error, make sure to only call the map() method on arrays or correct the type of the variable on which you call the method.
The AxiosResponse is the response object returned as a Promise due to a REST API call such as GET or POST . TypeScript.
The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names.
Make sure to install axios before using the code snippets in this article. You can install axios by opening your terminal in your project's root directory and running the npm install axios command. Copied! Axios includes TypeScript definitions, so we don't have to install them separately.
what is the type of result? .map () is a method on Arrays. It doesn't look like you are using rxjs, so I don't think the rxjs/operators import is needed. Axios Response does not only contain the response body, it is an object containing all the details about the request, the response, etc...
const AxiosClient = axios.create<APIResponse> ( { // ... }); and then use it like this: AxiosClient.post<string> ('/endpoint').then (value => value.data), value.data has type...
so, we want use interceptor add some property to AxiosResponse and have type inference, it's contradictory, because there is no way to ensure that type inference can be updated when interceptor is eject it's not 100% safety, but it's safety than just use axios.interceptors.response.use
It works fine with axios.create () without a generic type or just axios, but if I pass my interface like this: const AxiosClient = axios.create<APIResponse> ( { // ... }); and then use it like this: AxiosClient.post<string> ('/endpoint').then (value => value.data), value.data has type...
Axios Response does not only contain the response body, it is an object containing all the details about the request, the response, etc...
If you want to map the response body (data), you have to do all of this on 'response.data' and not 'response' :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With