Next.js Discord

Discord Forum

Api fetching error????

Unanswered
Black carp posted this in #help-forum
Open in Discord
Black carpOP
[ ] - info Generating static pages (9/12)node:internal/deps/undici/undici:7043
throw new TypeError("Failed to parse URL from " + input, { cause: err });
^

TypeError: Failed to parse URL from /api/getCookies
at new Request (node:internal/deps/undici/undici:7043:19)
at r (C:\Users\USER\Desktop\kwiatek-lekcje.next\server\chunks\366.js:6720:65)
... 2 lines matching cause stack trace ...
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:399:5)
at new URL (node:internal/url:560:13)
at new Request (node:internal/deps/undici/undici:7041:25)
at r (C:\Users\USER\Desktop\kwiatek-lekcje.next\server\chunks\366.js:6720:65)
at doOriginalFetch (C:\Users\USER\Desktop\kwiatek-lekcje\node_modules\next\dist\server\lib\patch-fetch.js:278:24)
at C:\Users\USER\Desktop\kwiatek-lekcje\node_modules\next\dist\server\lib\patch-fetch.js:415:20
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
input: '/api/getCookies',
code: 'ERR_INVALID_URL'
}
}

8 Replies

Black carpOP
async function getCookies() {
const res = await fetch("/api/getCookies");
return await res.json();
}

let UserRole = "standard";
getCookies().then((userCookie) => {
if (userCookie) {
isAdmin(JSON.parse(userCookie.user.value).email)
.then((isUserAdmin) => {
if (isUserAdmin.response == 1) {
UserRole = "admin";
} else {
UserRole = "standard";
}
})
.catch((error) => {
console.log("Błąd podczas sprawdzania roli użytkownika:", error);
});
console.log("User has properly logged in, his role is: " + UserRole);
} else {
console.log("User isn't logged in, or the cookie hasn't been set.");
router.push("/register");
}
});
Pretty sure fetch doesnt work with relative paths
Try using an absolute path
Black carpOP
didn't help
What didnt help exactly?
The url you are using in fetch is a relative url. You need to put the entire url in it.

/api/getCookies would become something like
http://localhost:3000/api/getCookies
Black carpOP
that din't help