Where does the id format get configured in "/invoice/[id]" in the tutorial?
Answered
Gharial posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
I'm going through the dashboard project tutorial for NextJS and am on Chapter 13: Handling Errors. I was working on implementing the notFound function and noticed something interesting
When I use this link
http://localhost:3000/dashboard/invoices/12/edit
When I use this link
http://localhost:3000/dashboard/invoices/2e94d1ed-d220-449f-9f11-f0bbceed9645/edit
Where is the id format defined?
Is this a uuid thing or a nextjs thing?
When I use this link
http://localhost:3000/dashboard/invoices/12/edit
/dashboard/invoices/error.tsx
would triggerWhen I use this link
http://localhost:3000/dashboard/invoices/2e94d1ed-d220-449f-9f11-f0bbceed9645/edit
/dashboard/invoices/[id]/edit/not-found.tsx
would triggerWhere is the id format defined?
Is this a uuid thing or a nextjs thing?
![Image](https://cdn.discordapp.com/attachments/1185374339147767848/1185374352045252608/Screenshot_2023-12-15_at_4.09.50_PM.png?ex=658f60e4&is=657cebe4&hm=69bedcec9f474b5d67fb06e3eeecd00c4a56ac7ff784802f588ca1e3acf5dead&)
![Image](https://cdn.discordapp.com/attachments/1185374339147767848/1185374352456290424/Screenshot_2023-12-15_at_4.10.02_PM.png?ex=658f60e4&is=657cebe4&hm=5140a7c8414cfac22673b6400dfc8d916704ee473b1f11c6f1969286c56ead1c&)
Answered by Ray
export async function fetchInvoiceById(id: string) {
try {
const data = await sql<InvoiceForm>`
SELECT
invoices.id,
invoices.customer_id,
invoices.amount,
invoices.status
FROM invoices
WHERE invoices.id = ${id};
`;
const invoice = data.rows.map((invoice) => ({
...invoice,
// Convert amount from cents to dollars
amount: invoice.amount / 100,
}));
return invoice[0];
} catch (error) {
console.error("Database Error:", error);
}
}
this is what i have, yes because you throw it
21 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
here's a link to the tutorial
https://nextjs.org/learn/dashboard-app
https://nextjs.org/learn/dashboard-app
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
I get 404 when visiting to http://localhost:3000/dashboard/invoices/12/edit
but when you get to the error page, it should be something wrong with the
but when you get to the error page, it should be something wrong with the
fetchInvoiceById
function![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
huh interesting. I'll check that out. Weird bug
did you by chance edit the
by default they have
while in the actions we define the errors like
did you by chance edit the
fetchInvoiceById
function?by default they have
export async function fetchInvoiceById(id: string) {
noStore();
try {
const data = await sql<InvoiceForm>`
SELECT
invoices.id,
invoices.customer_id,
invoices.amount,
invoices.status
FROM invoices
WHERE invoices.id = ${id};
`;
const invoice = data.rows.map((invoice) => ({
...invoice,
// Convert amount from cents to dollars
amount: invoice.amount / 100,
}));
return invoice[0];
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch invoice.');
}
}
while in the actions we define the errors like
} catch (error) {
return { message: 'Database Error: Faied to create invoice.' };
}
maybe because I'm throwing?
I doubt it though
I doubt it though
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
export async function fetchInvoiceById(id: string) {
try {
const data = await sql<InvoiceForm>`
SELECT
invoices.id,
invoices.customer_id,
invoices.amount,
invoices.status
FROM invoices
WHERE invoices.id = ${id};
`;
const invoice = data.rows.map((invoice) => ({
...invoice,
// Convert amount from cents to dollars
amount: invoice.amount / 100,
}));
return invoice[0];
} catch (error) {
console.error("Database Error:", error);
}
}
this is what i have, yes because you throw it
Answer
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
it should return null, if not found instead of throwing
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
Oh got it
i tried to
and that rendered out the
plainly console logging the error though allowed
i'm guessing because of what you're saying about needing to return null
i tried to
return { message: ... }
and that rendered out the
error.tsx
plainly console logging the error though allowed
not-found.tsx
to loadi'm guessing because of what you're saying about needing to return null
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
yep then handle it with
notFound
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
is that a quirk of a ... - i forget how we classify these here
they're services
but they're not actions
how do we call this kind of function?
they're services
but they're not actions
how do we call this kind of function?
thanks for the help btw
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
query? I don't get it 😆
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
like
while
trying to understand the distinction associated with the separation because they all fall under (C)reate,(R)ead,(U)pdate,(D)estroy
/lib/actions.tsx
are server actions for Create Update Deletewhile
/lib/data.tsx
are (you're right they totally are just get requests so queries make sense)trying to understand the distinction associated with the separation because they all fall under (C)reate,(R)ead,(U)pdate,(D)estroy
both working against the same db
just trying to understand their reasoning for separating the GET reqs vs everything else
just trying to understand their reasoning for separating the GET reqs vs everything else
makes sense
just never seen that distinction before
just never seen that distinction before
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
data layer? data access?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
im more accustomed to resource1/crud , resource2/crud
vs this being more
cud/all-resources
r/all-resources
vs this being more
cud/all-resources
r/all-resources
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
page = r
server action = cud
server action = cud
![:noice:](https://cdn.discordapp.com/emojis/753872185185009774.png)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
GharialOP
ill take it lol
appreciate the help ðŸ™
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
np please mark solution