-
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 16 replies
-
Hi, that's because if you use |
Beta Was this translation helpful? Give feedback.
-
Same problem I guess, Maybe this occur because I use i18n library? |
Beta Was this translation helpful? Give feedback.
-
@TheEdoRan I know the result using next redirect is undefined, but wouldn't using redirect on a regular server action not return undefined as a type? the official documentation does not discuss this. Isn't this getting confusing? |
Beta Was this translation helpful? Give feedback.
-
I threw together this helper function to handle this. It throws an error if the action response or response data are undefined. Feel free to modify to your needs! /**
* Helper function to use an action on the server for data fetching.
* Catches undefined responses.
* @throws Error
*/
export async function handleActionResult<
ServerError,
S extends Schema,
BAS extends readonly Schema[],
CVE,
CBAVE,
Data,
NextCtx,
>(
action: Promise<
SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data, NextCtx> | undefined
>
) {
const res = await action;
if (typeof res === "undefined") {
throw new Error("Action returned with undefined result.");
}
const data = res.data;
if (typeof data === "undefined") {
throw new Error("Action returned with data undefined.");
}
return { ...res, data };
} Usage: const brands = await handleActionResult(getVendorBrands()); |
Beta Was this translation helpful? Give feedback.
-
Hey all! Running into this as well at the moment (Latest version of next & next-safe-action). I have action which down every path returns a object, even going as far as to create a "dummy object" which pending validation errors returns a result every time. Nonetheless, even after checking all potential errors it still says that result.data could be undefined or the object. Is this intentional @TheEdoRan? |
Beta Was this translation helpful? Give feedback.
By default every server error gets caught by the library and a
serverError
is returned. The only exception is for Next.js navigation errors, which are instead caught by the framework. If you useredirect
theresult
will be undefined. TypeScript cannot infer anever
return, so the safest way for the end user is to type the result asobject | undefined
, as it is right now.