In the console it shows me a warning:
The entity passed to the selectId
implementation returned undefined. You should probably provide your own selectId
implementation. The entity that was passed: (2) [{…}, {…}] The selectId
implementation: item => item._id.
What am I missing?
I try to call the productos with:
const products = useSelector(productSelectors.selectIds)
import { createSlice, createEntityAdapter, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
//Fetch the businesses to show in the home page
export const fetchProducts = createAsyncThunk(
'products/fetchProducts',
async (id, { dispatch }) => {
return axios.get(`https://api-test-carrinho.herokuapp.com/product/business/${id}`
).then(resp => {
dispatch(addProducts(resp.data))
})
}
)
const productsAdapter = createEntityAdapter({
selectId: (item) => item._id
});
const productsSlice = createSlice({
name: "products",
initialState: productsAdapter.getInitialState({ loading: false }),
reducers: {
/* addProduct: productsAdapter.addOne, */
addProducts: productsAdapter.setAll
},
extraReducers: {
[fetchProducts.pending](state){
state.loading = true
},
[fetchProducts.fulfilled](state, { payload }){
state.loading = false
productsAdapter.setAll(state, payload)
}
}
});
export default productsSlice.reducer;
export const { /* addProduct, */addProducts } = productsSlice.actions;
export const productSelectors = productsAdapter.getSelectors(
(state) => state.products
);
export const {
selectById: selectProductById,
selectAll: selectAllProducts
} = productSelectors;