Next.js Discord

Discord Forum

Getting a not found instead of redirecting

Unanswered
Eastern yellowjacket posted this in #help-forum
Open in Discord
Eastern yellowjacketOP
Hi there, I am trying to delete a chat from my sidebar. I am on a url like /chat/xxxx. I want to delete the xxxx chat and redirect back to /chat. Here is the server action I am using:

export async function deleteChat(data: FormData) {
    const chatSessionId = data.get('chatSessionId');
    if (chatSessionId === null) {
        throw new Error('Chat ID');
    } else if (typeof chatSessionId !== 'string') {
        throw new Error('Chat ID is not a string');
    }
    try {
        await db
            .delete(chatSessions)
            .where(eq(chatSessions.chatSessionId, chatSessionId));
        revalidatePath('/chat');
        redirect('/chat', RedirectType.replace);
    } catch (err) {
        if (err instanceof Error) console.log(err.stack);
    }
}


After clicking delete, I just get a not found and my url is still at /chat/xxxx instead of /chat/. I think I can get around this by wrapping the server action call with an onSubmitHandler, and redirecting after the server action call in the client component, but I'm not sure why its not working with the current setup. Would appreciate any help - thanks for looking!

0 Replies