I'm trying to have my order history screen screen display the order history of a specific user. When I go to the path I have the error that is in the title. I made sure that the path is correct and that I spelled everything correctly but it's still coming back undefined. I also did console logs in the function and it's coming back with nothing which let's me know that the function isn't getting to the backend. I don't know what else to do. Below is the code for reference:
screen.js
import { axios } from "axios";
import React, { useContext, useEffect, useReducer } from "react";
import Button from "react-bootstrap/esm/Button";
import { Helmet } from "react-helmet-async";
import { useNavigate } from "react-router";
import LoadingBox from "../components/LoadingBox";
import MessageBox from "../components/MessageBox";
import { getError } from "../utils";
import { Store } from "./Store";
const reducer = (state, action) => {
switch (action.type) {
case "FETCH_REQUEST":
return { ...state, loading: true };
case "FETCH_SUCCESS":
return { ...state, orders: action.payload, loading: false };
case "FETCH_FAIL":
return { ...state, loading: false, error: action.payload };
default:
return state;
}
};
export default function OrderHistoryScreen() {
const { state } = useContext(Store);
const { userInfo } = state;
const navigate = useNavigate();
const [{ loading, error, orders }, dispatch] = useReducer(reducer, {
loading: true,
error: "",
});
useEffect(() => {
const fetchData = async () => {
dispatch({ type: "FETCH_REQUEST" });
try {
const { data } = await axios.get(
`/api/orders/mine`,
{ headers: { Authorization: `Bearer ${userInfo.token}` } }
);
dispatch({ type: "FETCH_SUCCESS", payload: data });
} catch (error) {
dispatch({
type: "FETCH_FAIL",
payload: getError(error),
});
}
};
fetchData();
}, [userInfo]);
return (
<div>
<Helmet>
<title>Order History</title>
</Helmet>
<h1>Order History</h1>
{loading ? (
<LoadingBox></LoadingBox>
) : error ? (
<MessageBox variant="danger">{error}</MessageBox>
) : (
<table className="table">
<thead>
<tr>
<th>ID</th>
<th>DATE</th>
<th>TOTAL</th>
<th>PAID</th>
<th>DELIVERED</th>
<th>ACTIONS</th>
</tr>
</thead>
<tbody>
{orders.map((order) => {
<tr key={order._id}>
<td>{order._id}</td>
<td>{order.createdAt.substring(0, 10)}</td>
<td>{order.totalPrice.tofixed(2)}</td>
<td>{order.isPaid ? order.paidAt.substring(0, 10) : "No"}</td>
<td>
{order.isDelivered
? order.deliveredAt.substring(0, 10)
: "No"}
</td>
<td>
<Button
type="button"
variant="light"
className="cusButton"
onClick={() => {
navigate(`/order/${order._id}`);
}}
>
Details
</Button>
</td>
</tr>;
})}
</tbody>
</table>
)}
</div>
);
}
orderRoutes.js
const orderRouter = express.Router();
orderRouter.get(
"/mine",
isAuth,
expressAsyncHandler(async (req, res) => {
const orders = await Order.find({ user: req.user._id });
if (orders) {
console.log("these are the orders: " orders);
res.send(orders);
} else {
console.log("Order not found");
}
})
);
orderRouter.get(
"/:id",
isAuth,
expressAsyncHandler(async (req, res) => {
const order = await Order.findById(req.params.id);
if (order) {
res.send(order);
} else {
res.status(404).send({ message: "Order Not Found" });
}
})
);
export default orderRouter;
CodePudding user response:
Change your axios
import statement to:
import axios from "axios";