Skip to content

TypeError: Cannot read properties of undefined (reading 'clone') #5045

@preetish-brine

Description

@preetish-brine

Error Details

  • Level: Error
  • Cannot read properties of undefined (reading 'clone')
  • Location: (@reduxjs/toolkit/dist/query/rtk-query.esm)
Image

Source Map

../../node_modules/@reduxjs/toolkit/dist/query/rtk-query.esm.js in at line 269:46
../../node_modules/@reduxjs/toolkit/dist/query/rtk-query.esm.js in step at line 23:23
../../node_modules/@reduxjs/toolkit/dist/query/rtk-query.esm.js in Object.next at line 4:53
../../node_modules/@reduxjs/toolkit/dist/query/rtk-query.esm.js in fulfilled at line 70:32

Our Setup
We’re using:

const baseQueryWithSessionHandler = async (args, api, extraOptions) => {
  ...
  const result = await baseQuery(adjustedArgs, api, extraOptions);
    if (result.data) {
      result.data = { ...result.data, statusCode: result.meta.response.status };
      return result;
    }
  ...
};

Wrapped in retry() and custom backoff:

const staggeredBaseQueryWithSessionHandler = retry(baseQueryWithSessionHandler, {
  backoff: customBackoff,
  maxRetries: 3,
  retryCondition: (error, args, { baseQueryApi, attempt }) => {
    return (
      args.method === 'GET' &&
      attempt < 3 &&
      error?.data?.message !== unauthorizedErrorMessage &&
      error?.status !== 401
    );
  },
});

After digging into the source of fetchBaseQuery and retry, we observed:
meta is created and initially populated with request:

requestClone = request.clone();
meta = { request: requestClone };
Later, only if fetch() succeeds, response.clone() is called and attached to meta.response.

responseClone = response.clone();
meta.response = responseClone;
If the initial fetch fails (e.g. due to network or DNS issues), then meta.response is never set.

If a subsequent retry succeeds, the returned result includes data, but meta.response may still be missing

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions