I’m trying to send a GET request to an API to fetch courses based on a category specified in the URL. I’m using the fetch API in JavaScript to do this, but I’m encountering an error. The code I’m using is as follows:
// Function for getting key from URL
const getUrlParams = (key) => {
const urlParams = new URLSearchParams(location.search);
return urlParams.get(key);
};
// Function to fetch courses based on category
const getAndShowCategoryCourses = async () => {
let categoryName = getUrlParams("cat");
const res = await fetch(
`http://localhost:4000/v1/courses/category/${categoryName}`
);
const courses = await res.json();
return courses;
};
Expected Behavior: The function should fetch and log the list of courses based on the cat parameter from the URL.
Actual Behavior: Instead of fetching the courses, I get the error:
[ “message”: “this route is protected and you can’t have access to it.”]
Browser/Environment: I’m testing this on Chrome and the backend tech are node and MongoDB.
Backend Details: The API is running on a local server at http://localhost:4000/v1/courses/category/:categoryName.
Steps Taken:
I verified that the cat parameter is correctly retrieved using getUrlParams
,
and when I’m testing this API using postman with this API (http://localhost:4000/v1/courses/category/backend) and it gives me the same error:
[ “message”: “this route is protected and you can’t have access to it.”]