Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding headers to query throws Type Error: Cannot read properties of undefined (reading 'has') #2773

Closed
trevans24 opened this issue Oct 14, 2022 · 2 comments

Comments

@trevans24
Copy link

package.json:
"@reduxjs/toolkit": "^1.8.6",
"react-redux": "^8.0.4"

stack:
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1508:26
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1406:26
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1359:22
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1270:26
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1230:22
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1181:26
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1566:26
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/query/rtk-query.esm.js:1600:24
at eval (webpack-internal:///./node_modules/
reduxjs/toolkit/dist/redux-toolkit.esm.js:427:22
eval
webpack-internal:///./node_modules/redux-thunk/es/index.js:21:16

store.js

import { configureStore } from '@reduxjs/toolkit'

import { exploreApi } from 'ui/explore/exploreApi'

const store = configureStore({
  middleware: getDefaultMiddleware => getDefaultMiddleware().concat(exploreApi.middleware),
  reducer: {
    [exploreApi.reducerPath]: exploreApi.reducer
  },
})

export default store

exploreApi:

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import Session from 'services/session'

const HOST = process.env.NEXT_PUBLIC_YOU_API_HOST || 'http://localhost:8000'

export const exploreApi = createApi({
  baseQuery: fetchBaseQuery({
    baseUrl: `${HOST}`,
    prepareHeaders: (headers) => {
      headers.set('Accept', 'application/json')
      headers.set('X-Session-Id', new Session().get())
    }
  }),
  endpoints: ({ query }) => ({
    getNewestCards: query({
      providesTags: (res, err, id) => [{ id, type: 'cards' }],
      query: page => `/v2/cards/newest?take=${page * 12}` 
    })
  }),
  reducerPath: 'exploreApi',
  tagTypes: ['cards']
})

export const { useGetNewestCardsQuery } = exploreApi

Session:

class Session {
  constructor(sessionKey = 'sessionId') {
    this.key = sessionKey
  }

  save(v) {
    if (typeof window !== 'undefined') {
      const disk = localStorage

      if (disk) {
        disk.setItem(`@you:${this.key}`, JSON.stringify(v))
      }
    }
  }

  clear() {
    this.save(null)
  }

  get() {
    return this.sessionId || this.load()
  }

  load() {
    let data = null
    if (typeof window !== 'undefined') {
      const disk = localStorage

      if (disk) {
        try {
          this.sessionId = data = JSON.parse(disk.getItem(`@you:${this.key}`))
        } catch (e) {
          throw new Error()
        }
      }
    }
    return data
  }
}

export default Session
@markerikson
Copy link
Collaborator

I believe you need to return headers from inside of prepareHeaders():

@trevans24
Copy link
Author

Ah yes, dumb me... I looked at this over and over again. Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants