访问redux返回的对象属性时出错-无法读取未定义的属性'id' 我有一个由redux select函数返回的对象,我可以注销该对象
console.log(category)
但是当我尝试访问其属性时,它会引发错误-无法读取未定义的属性'id'
console.log(category.id)
请帮助
CategorySlice.js 从'@ reduxjs / toolkit'导入{createSlice,createEntityAdapter,createAsyncThunk,createReducer} 从“ axios”导入axios
export const getCategoryData = createAsyncThunk('category/getProductData', async id => {
const category = await axios.get(
`http://localhost:8000/category/${id}`
)
return category.data
})
export const categoryAdapter=createEntityAdapter({})
export const {
selectById: selectCategoryById
} = categoryAdapter.getSelectors(state => state.inventory.categories.category)
const selectedCategorySlice = createSlice({
name: 'category',
initialState:categoryAdapter.getInitialState(),
reducers: {
// selectedCategory:(state,action)=>state.push(action.payload)
},
extraReducers: {
// [getCategoryData.fulfilled]: (state, action) => action.payload
// createReducer(initialState,
// [getCategoryData.fulfilled]
[getCategoryData.fulfilled]: categoryAdapter.addOne
}
})
export const { selectedCategory } = selectedCategorySlice.actions
export default selectedCategorySlice.reducer
Category.js
const category1 = useSelector(state => selectCategoryById(state, id))
console.log(category1)-->Working-print the object
console.log(category1.id)--> This gives an error Cannot read property 'id' of undefined