微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Redux工具箱-无法在upsertMany上使用'in'运算符错误

如何解决Redux工具箱-无法在upsertMany上使用'in'运算符错误

我正在将文档upsertMany放入我的Redux createSlice中。

我正在关注文档的this部分。

我在upsertMany电话中不断收到此错误这是为什么?

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

我注意到normalizr同时返回entitiesresult对象,但是RTK仅使用entities。如果需要,我们在哪里使用result

这是我的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

这是归一化的对象


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。