From 05ce71b3934bfeefafb3d7aa1bf677a4dd86c5a9 Mon Sep 17 00:00:00 2001 From: Mandeep Singh Date: Tue, 2 Jul 2024 17:55:26 +0530 Subject: [PATCH] Fixes the bug in case if a user is not found in the db --- app/(root)/profile/[profileId]/page.tsx | 27 ++++++++++++++++++------- convex/users.ts | 6 ++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/(root)/profile/[profileId]/page.tsx b/app/(root)/profile/[profileId]/page.tsx index d77f916..4dd0895 100644 --- a/app/(root)/profile/[profileId]/page.tsx +++ b/app/(root)/profile/[profileId]/page.tsx @@ -1,5 +1,6 @@ "use client"; +import { useEffect, useState } from "react"; import { useQuery } from "convex/react"; import EmptyState from "@/components/EmptyState"; @@ -8,22 +9,34 @@ import PodcastCard from "@/components/PodcastCard"; import ProfileCard from "@/components/ProfileCard"; import { api } from "@/convex/_generated/api"; import { ProfilePodcastProps } from "@/types"; - const ProfilePage = ({ params, }: { params: { profileId: string; }; -}) => { - const user = useQuery(api.users.getUserById, { - clerkId: params.profileId, - }); + }) => { + + const [isFetching, setIsFetching] = useState(true); + const user = useQuery(api.users.getUserById, { clerkId: params.profileId }); const podcastsData = useQuery(api.podcasts.getPodcastByAuthorId, { authorId: params.profileId, }) as ProfilePodcastProps; - if (!user || !podcastsData) return ; + useEffect(() => { + if (user || user === null) { + setIsFetching(false); + } + }, [user]); + + if (isFetching) return ; + + if (!isFetching && !user) + return ( +
+ +
+ ); return (
@@ -33,7 +46,7 @@ const ProfilePage = ({
diff --git a/convex/users.ts b/convex/users.ts index 5c3ea56..55ed7db 100644 --- a/convex/users.ts +++ b/convex/users.ts @@ -41,7 +41,8 @@ export const getUserById = query({ .unique(); if (!user) { - throw new ConvexError("User not found"); + // throw new ConvexError("User not found"); + return null; } return user; @@ -76,7 +77,8 @@ export const getSubscriptionByClerkId = query({ .first(); if (!user) { - throw new ConvexError("User not found"); + // throw new ConvexError("User not found"); + return null; } return {