Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remix Error: You defined an action for route "routes/" but didn't return anything from your action function

I am using Remix for my project and I am currently getting this error:

Error: You defined an action for route "routes/demo" but didn't return anything from your action function. Please return a value or null.

The error message sounds quite straight forward, but I can't put a return when I am already throwing an error. I noticed this happen when name is John.

But this is a mock of my action code:

export async function action({ request }: ActionFunctionArgs) {
  const formData = await request.formData();
  try {
    if (formData.get(“name”) === “John”) {
      throw new Response(“Name is not allowed”, { status: 400 });
    }
    return { success: true }
  } catch (e) {
    console.log(e);
  }
}
like image 396
user30312022 Avatar asked Oct 26 '25 05:10

user30312022


1 Answers

You need to include a return in your catch block. Whenever the name is equal to John, the code gets handed over to the catch block (which doesn't have a return), that's why you are getting the error.

This:

export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
  try {
       if (formData.get(“name”) === “John”) {
      throw new Response(“Name is not allowed”, { status: 400 });
    }
  } catch (e) {
    console.log(e);
    return {error:{message:"Error"}} // return null or whatever value you would like to return 

  }
}
like image 59
Hilory Avatar answered Oct 27 '25 17:10

Hilory