0

I am using Redux for state management, I have faced an issue in reducer function here is the image of my console, You can see the Product Action is providing my data but the reducer is not passing on my function

enter image description here

here is my code of ProductAction:

export const getProductsbyFind = (myvariable) =>async (dispatch)=>{
    try {
        console.log(myvariable)
        dispatch({type: ALL_PRODUCTS_REQUEST_BY_ID})
        const{ data } = await axios.get(`/api/v1/product/${myvariable}`)
        console.log(data)
        dispatch({
            type: ALL_PRODUCTS_SUCCESS_BY_ID,
            payload: data
        })
        
    } catch (error) {
        dispatch({
            type:ALL_PRODUCTS_FAIL,
            payload: error.response.data.message
        })
        
    }
}

here is the code of Reducer:

export const productReducersById = (state = { products: [] }, action) => {
    switch (action.type) {
        case ALL_PRODUCTS_REQUEST_BY_ID:
            return {
                loading: true,
                products: []
            }
        case ALL_PRODUCTS_SUCCESS_BY_ID:
            return {
                loading: false,
                products: action.payload.products,
                productsCount: action.payload.productsCount
            }
        case UPDATE_QUANTITY_BY_ID:
            const { index, quantity } = action.payload;
            const prods = state.products.map((p, i) => {
                if (i !== index)
                    return p;
                return {
                    ...p,
                    quantity
                }
            });
            return {
                loading: true,
                products: prods
            }

        case ALL_PRODUCTS_FAIL_BY_ID:
            return {
                loading: false,
                error: action.payload
            }
        case CLEAR_ERRORS_BY_ID:
            return {
                ...state,
                error: null
            }
        default:
            return state
    }
}

here is the code of my page where I want to get my data:

  const { loading, products, error, productCount } = useSelector(state => state.products);
  console.log(products)
  useEffect(() => {
    dispatch(getProductsbyFind(myvariable));
  }, [dispatch])
2
  • I think it's a spelling error. Your response has product as a key name but your reducer is accessing it via the action.payload.products Commented Mar 9, 2022 at 8:51
  • kindly mention me where is the mistake Commented Mar 9, 2022 at 9:03

1 Answer 1

2

You have a typo in your reducer:

  case ALL_PRODUCTS_SUCCESS_BY_ID:
      return {
          loading: false,
-         products: action.payload.products,
+         products: action.payload.product,
          productsCount: action.payload.productsCount
      }

(Also, productsCount does not exist in your payload, so that will become undefined.)

Sign up to request clarification or add additional context in comments.

4 Comments

still same console result
i removed productsCount and action.payload.products but still same console
Also what happens in your example is that your reducer assumes that products are all arrays. Your example code is just a single product object. So what happens is that you change your array into an object. You probably want to have products: [action.payload.product]. Or if you want to keep previous products, you want products: [...state.products, action.state.product]
got it ... it's really helpful for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.