ReferenceError: Cannot access 'api' before initialization

See original GitHub issue

I am having trouble catching the action generated from the injectEndpoints from the workspace api in my userSlice using the extraReducers.

My code structure is as follow.

baseApi src/redux/services/api

export const api = createApi({
  reducerPath: "api",
  baseQuery: baseQueryWithReauth,
  endpoints: (builder) => ({
  }),
});

Workspace src/redux/services/workspace

export const workspace = api.injectEndpoints({
  endpoints: (builder) => ({
    getDocs: builder.query<any, void>({
      query: () => `/workspace/6236f159662bebb0c2669430/docs`,
    }),
  }),
});

export const { useGetDocsQuery } = workspace;

User Slice src/redux/splices/user

const userSlice = createSlice({
  name: "user",
  initialState: { user: null } as UserState,
  reducers: {
    setUser: (state, action) => {
      state.user = action.payload.user;
    },
    loggedOut: (state) => {
      state.user = null;
    },
  },
  extraReducers: (builder) => {
    builder.addMatcher(
      workspace.endpoints.getDocs.matchFulfilled,
      (state, { payload }) => {
        console.log(payload);
      }
    );
  },
});

The error : ReferenceError: Cannot access ‘api1’ before initialization This Line : export const workspace = api.injectEndpoints({

If I put the code from the workspace api directly under the baseApi, it works without any issues so I am not sure how to solve this problem.

Edit : It looks like a circular dependency but I can’t seem to figure out how to sole it if the code is in another file.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
silent-tancommented, Jul 14, 2022

just the code like https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#automatic-re-authorization-by-extending-fetchbasequery

it works if remove

api.dispatch(logout())

maybe works if not use injectEndpoints api

0reactions
silent-tancommented, Dec 12, 2022

No, i had solved this problem by subscribing in extraReducer

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: Cannot access before initialization in JS
The "Cannot access before initialization" error occurs when a variable declared using let or const is accessed before it was initialized in the...
Read more >
ReferenceError: can't access lexical declaration 'X' before ...
The JavaScript exception "can't access lexical declaration `variable' before initialization" occurs when a lexical variable was accessed ...
Read more >
node.js - ReferenceError: Cannot access 'fs' before initialization
The error means that you're trying to use the variable fs BEFORE its const fs = ... or let fs = ... definition...
Read more >
referenceerror: cannot access 'main' before initialization
The “cannot access before initialization” reference error occurs in JavaScript when you try to access a variable before it is declared with let...
Read more >
Cannot access 'XXX' before initialization - r/angular ... - Reddit
Uncaught ReferenceError: Cannot access 'XXX' before initialization. Hello I get this error message when I try to import a component into another ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found