Skip to content
Back

Unwanted Error Page Redirects in Next.js with Appwrite on "Document Not Found"

  • 0
  • Web
Vineet
7 Jun, 2024, 06:13

Environment:

TypeScript
Appwrite Version: 1.5.7
Framework: Next.js (latest)

Issue: In a Next.js app, getCurrentUserInfo redirects to an error page when a document is not found, even though it logs the error and returns null. This contrasts with getLoggedInUser, which handles similar errors correctly by only logging and returning null.

TypeScript
export const getCurrentUserInfo = async (id: string) => {
    try {
        const { databases } = await createDatabaseClient();
        const db = await getDatabaseClient(databases);
        const userInfo = await db.users.get(id);
        if (!userInfo || userInfo.deleted) return null;
        return userInfo;
    } catch (error) {
        console.error("Error:", error);
        return null;  // Unwanted redirect happens here
    }
};

const Home = async () => {
    const user = await getLoggedInUser();
    const userInfo = await addUserInfoToDB(user);
    return <div>{user && <Component user={user} />}</div>;
}

Behavior:

TypeScript
getCurrentUserInfo causes a redirect on errors.
getLoggedInUser functions correctly without redirects.

Question: How can I avoid these unwanted redirects while maintaining consistent error handling?

Any help or insights would be appreciated!

TL;DR
Developers using Next.js with Appwrite are experiencing unwanted error page redirects when a document is not found, specifically in the getCurrentUserInfo function. The getLoggedInUser function works correctly without redirects. The issue seems to be related to how errors are handled in getCurrentUserInfo. One potential solution would be to add a check for the "Document Not Found" error specifically in the catch block and handle it differently to prevent the redirect.
Vineet
7 Jun, 2024, 06:14
TypeScript
export async function getLoggedInUser() {
    try {
        const { account } = await createSessionClient()
        return await account.get()
    } catch (error) {
        console.error(error)
        return null
    }
}
Vineet
7 Jun, 2024, 06:14

This login function works properly without redirecting to error page

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more