问题描述
我正在将文档upsertMany
放入我的Redux createSlice
中。
我正在关注文档的this部分。
我在upsertMany
电话中不断收到此错误。 这是为什么?
Unhandled Rejection (TypeError): Cannot use 'in' operator to search for '525' in undefined
我注意到normalizr同时返回entities
和result
对象,但是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 (将#修改为@)