Why useRouteData can return undefined?

In this example:
export function routeData({ params }: RouteDataArgs) {
return createServerData$(
async ([_, id], { request }) => {
const user = await authenticator.isAuthenticated(request, {
failureRedirect: "/",
});

if (!user.isAdmin) {
throw redirect("/login");
}

return prisma.organisation.findUniqueOrThrow({
where: { id },
});
},
{ key: () => ["organisation", params.orgId] }
);
}

export default function AdminOrganisations() {
const organisation = useRouteData<typeof routeData>();

return (
<>
<Header>{organisation().name}</Header>
<Container>...</Container>
</>
);
}
export function routeData({ params }: RouteDataArgs) {
return createServerData$(
async ([_, id], { request }) => {
const user = await authenticator.isAuthenticated(request, {
failureRedirect: "/",
});

if (!user.isAdmin) {
throw redirect("/login");
}

return prisma.organisation.findUniqueOrThrow({
where: { id },
});
},
{ key: () => ["organisation", params.orgId] }
);
}

export default function AdminOrganisations() {
const organisation = useRouteData<typeof routeData>();

return (
<>
<Header>{organisation().name}</Header>
<Container>...</Container>
</>
);
}
If I'm not an admin I'm redirected. If no organisation is found with the id, prisma throw. So I'm sure const organisation should be defined. But the type is still
const organisation: Resource<Organisation | undefined>
const organisation: Resource<Organisation | undefined>
Why?
7 Replies
binajmen
binajmenOP2y ago
Should I really enclose JSX with Show/Suspence to assert the value of organisation? It seems boilerplaty, so I'm wondering if I must do something else..
binajmen
binajmenOP2y ago
<Show when={story()}>
<div class="item-view">
<div class="item-view-header">
<a href={story()!.url} target="_blank">
<Show when={story()}>
<div class="item-view">
<div class="item-view-header">
<a href={story()!.url} target="_blank">
story()!?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
binajmen
binajmenOP2y ago
feels hacky ^^'
ryansolid
ryansolid2y ago
There is a solution for this coming in 2.0, it'd just be breaking right now. Mind you I will probably still use the ! in my code
binajmen
binajmenOP2y ago
I think it's just a barrier to break, ! exists for a reason and this one is a valid one to use. Counter intuitive, so I was wondering if I was doing something wrong ^^
Want results from more Discord servers?
Add your server