I get the following status codes in the terminal with my code but do not understand why. Here is the code. I believe it may be an issue with the folder structure but I am not sure. I have debugged and see no issues with any values be undefined, and see no such errors in terminal. I get these status codes also:
GET /api/Project?userId=6656e135c761e2d4bace9ef7&sortKey=createdAt&sortOrder=desc 500 in 59ms
GET /api/Project/User/6656e135c761e2d4bace9ef7 404 in 127ms
ProjectList.jsx
useEffect(() => {
const selectedColumn = Object.keys(sortMap).find((key) => sortMap[key]);
const selectedOrder = Object.values(sortMap).find((val) => null !== val);
try {
const sortedProjects = getSortedProjects(
currentUser.id,
selectedColumn,
selectedOrder
);
setProjects(sortedProjects);
setRefreshingProject(true);
} catch (error) {
throw new Error("Failed to sort Projects", error);
}
}, [sortMap]);
api.js
export async function getSortedProjects(userId, column, order) {
try {
const response = await axios.get(
`/api/Project?userId=${userId}&sortKey=${column}&sortOrder=${order}`
);
return response.data.projects;
} catch (error) {
throw new Error("Failed to get Projects by User id");
}
}
route.js –> /app/api/Project
export async function GET(req) {
const url = new URL(req.url);
const searchParams = new URLSearchParams(url.search);
const userId = searchParams.get("userId");
const sortKey = searchParams.get("sortKey");
const sortOrder = searchParams.get("sortOrder");
try {
let sortObj = {};
if (sortOrder === "desc") {
sortObj[sortKey] = -1;
} else {
sortObj[sortKey] = 1;
}
const projects = await Project.find({ userId: userId }).sort(sortObj);
return new NextResponse.json({ projects });
} catch (error) {
return NextResponse.json({ message: "Error", error }, { status: 500 });
}
}
I have tried putting the endpoint in Project > User > [id] and refactoring so that id comes from params and not searchParams of a new URL.
Have console.logged a lot and see no reason why I shouldn’t get the results other than the route endpoint, which I cannot see why doesn’t work.