Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'map' does not exist on type 'AxiosResponse<any>'.ts(2339)

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?

like image 374
Arte Avatar asked Jun 18 '20 13:06

Arte


People also ask

Does not exist on type map?

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.

What is AxiosResponse?

The AxiosResponse is the response object returned as a Promise due to a REST API call such as GET or POST . TypeScript.

Does not exist on type 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.

Does Axios work with TypeScript?

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 map() in Axios response?

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...

How to use axiosclient with apiresponse?

const AxiosClient = axios.create<APIResponse> ( { // ... }); and then use it like this: AxiosClient.post<string> ('/endpoint').then (value => value.data), value.data has type...

Can interceptor add some property to axiosresponse and have type inference?

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

How do I use axiosclient without a generic type?

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...


1 Answers

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' :)

like image 190
YanouHD Avatar answered Sep 30 '22 02:09

YanouHD