Redux toolkit - Cannot use 'in' operator error on upsertMany

539 views Asked by At

I am following the docs to upsertMany into my redux createSlice.

I am following this part of the docs.

I keep getting this error in my upsertMany call. Why is this?

Unhandled Rejection (TypeError): Cannot use 'in' operator to search for '525' in undefined

I have noticed that normalizr returns both entities and result objects, however, RTK only uses entities. Where do we use the result if we need to at all?

Here is my createSlice


const posts = new schema.Entity('actionPosts', {}, { idAttribute: 'id' })

export const fetchPosts = createAsyncThunk(
  'actionPosts/fetchPosts',
  async (email) => {
    const { data } = await getUserActionPostsByUser({ email })
    const extractedPosts = data.userActionPostsByUser
    const normalizedData = normalize(extractedPosts, [posts])
    return normalizedData.entities
  }
)

const adapter = createEntityAdapter({
  sortComparer: (a, b) => b.createdAt.localeCompare(a.createdAt),
  loading: '',
  error: '',
})

const initialState = adapter.getInitialState()

const slice = createSlice({
  name: 'actionPosts',
  initialState,
  extraReducers: {
    [fetchPosts.fulfilled]: (state, { payload }) => {
      console.log('payload', payload.actionPosts)
      adapter.upsertMany(state, payload.actionPosts) // error happens here
    },
  },
})

export default slice.reducer

Here is the normalized object


{
  actionPosts: {
      525: {
        id: 525
        email: "[email protected]"
        content: "lorem ipsum"
        createdAt: "2020-09-24T20:29:44.848Z"
      }
    }
  result[
    525,
  ]
}
0

There are 0 answers