#notFound() function
30 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
Are you using a try catch somewhere
Cause its going to intercept notFound()
NextJS uses exceptions as an easy way to throw signals arounds down the stack
nope
but the function runs inside a .catch
for a fetch request
Yeah so thats probably what is catching it maybe
Promise.all([
axios.get(`/api/bot/guild?id=${guildID}`, {signal: controller.signal}),
axios.get(`/api/db/guild?id=${guildID}`, {signal: controller.signal}),
axios.get(`/api/bot/guilds`, {signal: controller.signal}),
axios.get(`/api/db/notifications`, {signal: controller.signal})
]).then(function (response: Array<any>) {
if (response) {
setData({
bot: response[0]?.data,
db: response[1]?.data
});
setGuilds(response[2]?.data);
setNotifications(response[3]?.data);
}
}).catch(error => {
return handleErrorCodes(error.response, router);
});
what could I change to fix it?
export function handleErrorCodes(response: { status: number, data: any }, router: any) {
if (response) {
if (response.status === 401) {
router.push(response.data.redirect);
toast.error("You are not authorized to access that page");
} else if (response.status === 429) toast.warning("You are being ratelimited");
else if (response.status === 404) notFound();
}
return;
}
Just check if you the data isnt set and do a notFound
so I should put it like outside the useEffect or what
useEffect(() => {
const controller = new AbortController();
Promise.all([
axios.get(`/api/bot/guild?id=${guildID}`, {signal: controller.signal}),
axios.get(`/api/db/guild?id=${guildID}`, {signal: controller.signal}),
axios.get(`/api/bot/guilds`, {signal: controller.signal}),
axios.get(`/api/db/notifications`, {signal: controller.signal})
]).then(function (response: Array<any>) {
if (response) {
setData({
bot: response[0]?.data,
db: response[1]?.data
});
setGuilds(response[2]?.data);
setNotifications(response[3]?.data);
}
}).catch(error => {
return handleErrorCodes(error.response, router);
/*if (error.response?.status === 401) {
router.push(error.response?.data.redirect);
toast.error("You are not authorized to access that page");
} else if (error.response?.status === 429) toast.warning("You are being ratelimited");*/
});
return () => controller.abort();
}, [guildID]);
or maybe I can place a function and run it
Idk why but this seems overly complicated
Why is promise all being used?
And why axios over the normal fetch?
🍝
for them to complete faster
instead of running in a sequence
original dev used axios and it stayed like this
All fetches inside the same component are done in parallel:
https://nextjs.org/docs/app/building-your-application/data-fetching/patterns#parallel-data-fetching
You should really consider changing over to fetch one of these days cause that looks like spaghetti code
so much difference compared to nodejs
but this also got promise.all
Yes im dumb, i forgot u need to resolve the promise as well cause those are async
Bump
notFound() must be called during component rendering, not in event handlers like you did.
If you don’t understand the above statement, just consider that you cannot run notFound() in client components. Instead router.push() to a non existent route like router.push("/404")
Alright, I will use router.replace then