Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RTK Query get object inside result

my getMovies returns object data but what i need is data.result inside data. How can I directly get data.result?

My API: https://movie-flask.c3-na.altogic.com/movies

My code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export const movieApi = createApi({
    reducerPath: 'movieApi',
    baseQuery: fetchBaseQuery({
        baseUrl: 'https://movie-flask.c3-na.altogic.com/'
    }),
    tagTypes: [],
    endpoints: (builder) => ({
        getMovies: builder.query({
            query: () => "movies",
        })
    }),
})

// Export hooks for usage in functional components
export const { useGetMoviesQuery } = movieApi

I have tried

getMovies: builder.query({
   query: () => "movies",
}).result

It does not work.

Below is how i call useGetMoviesQuery

const { data, error, isLoading } = useGetMoviesQuery();

{data && data.result && data.result.map((item, i) => (
    <Text key={i}>
         {item.name}
    </Text>
))}

I want to get rid of data.result and directly want to call it with data.map.

Thanks.

>Solution :

You can use transformResponse:

getMovies: builder.query({
    query: () => "movies",
    transformResponse: response => {
      return response.result;
    },
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading