5

I am making a POST call using the RTK query. I am able to pass the payload data to my query, But I want to access the query arguments 'id' inside my transformResponse function. but found no ways to do so.

below is my code

export const apiSlice = createApi({
  reducerPath: 'api',
  
  baseQuery: fetchBaseQuery({ baseUrl: '/fakeApi' }),
 
  endpoints: builder => ({
    
    getPosts: builder.query({
      
      query: ({id}) => ({ url, method: 'POST', body: { id } }),
      transformResponse: (response) => { console.log(response, id) } //How can I access the 'id' from query here?
    })
  })
})

How can I access the query payload inside the transformResopnse function ?

I also tries passing the arg to transformResponse function but arg is undefined for me.

(response, meta, arg)

arg is undefined for me.

1 Answer 1

4

Make sure you are using at least the version v1.7.0 of redux-toolkit

Here is the parameters that takes transformResponse

(
  baseQueryReturnValue: unknown,
  meta: unknown,
  arg: unknown
)

You can use:

  • the third argument to get arg that contain your id
transformResponse: (response, meta, arg) => {
  console.log(response, meta, arg)
}
Sign up to request clarification or add additional context in comments.

7 Comments

I tries this but arg is undefined for me
have you try to use meta ?
yes, meta contains request and response object but no payload data
Are you using at least the version 1.7.0 github.com/reduxjs/redux-toolkit/releases/tag/v1.7.0 ?
Please upgrade this is why it does not work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.