Array turning into object for no reason when using Redux

Viewed 46

Im using redux to fetch orders from firebase , but after i try to use that array it says that its not an array ! i tried consoling log the type of it , and its telling me its an object , but i never changed it ! ordersArray

actions :

export const fetchOrders = () => {
  return async (dispatch) => {
    const ordersArray = [];
    const q = query(
      collection(db, "notValidatedOrders"),
      where("clientId", "==", authentication.currentUser.uid)
    );
    const querySnapshot = await getDocs(q);

    querySnapshot.forEach((doc) => {
      ordersArray.push(2);
    });

    console.log("ACTIONS", typeof ordersArray);

    dispatch({
      type: FETCH_ORDERS,
      orders: ordersArray,
    });
  };
};

Reducer :

import { FETCH_ORDERS } from "../actions/orders";

const initialState = {
  orders: [],
};

export default (state = initialState, action) => {
  switch (action.type) {
    case FETCH_ORDERS:
      return {
        orders: action.orders,
      };
  }
  return state;
};

the component that im using it in :



const orders = useSelector((state) => state.orders.orders);
 const [ordersState, setOrdersState] = useState([]);
 const [isLoading, setIsLoading] = useState(false);
 console.log(typeof orders);

 const fetchOrders = useCallback(async () => {
   setIsRefreshing(true);
   try {
     await dispatch(ordersActions.fetchOrders());
   } catch (err) {
     console.log(err.message);
   }
   setIsRefreshing(false);
 }, [setIsRefreshing, dispatch]);

 useEffect(() => {
   setIsLoading(true);
   fetchOrders()
     .then(() => {
       setOrdersState(orders);
     })
     .then(() => {
       setIsLoading(false);
     });
 }, [dispatch, fetchOrders]);
0 Answers
Related