Next.js Discord

Discord Forum

Server Actions returning a response? (login forms etc.)

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Is there anyway with RSC and Server Actions to return responses?
Success, Errors, input validation feedback ?

If I try login, and it fails, nothing happens, I can't find a way to display something that says "oops something went wrong etc."

if success then
if error like x then

It's probably something obvious that I'm missing, but since server actions are used with forms, there is probably some primitive that provides feedback when forms are submitted?

3 Replies

Barbary LionOP
Been afk for a bit, do we know if any update on this ? How are people using Server actions for forms without any validaiton or response logic ? I switched to client components, but curious if any news on this front
European hornet
I didn't know that this was an actual issue. I resolved this in my own app. Where my client form passes input to a server action which uses my Prisma DB. The input may not be correct, so we do not create or change anything. Instead we return an object with the Error indication { "error" : "some_error" }. I can read this object from the client side, and then raise the error to the user. It does fail if I try to raise the exception in the server or try to pass the server exception from server to the client. Where exactly are you guys running into the issue here?
Polar bear
Sure, server actions can return a reposne (error, success and even data!). Nothing special on server action side, just return {} For calling compnent, you need to convert it to client componet and than write a wrapper function to call server action. Have a look at https://createappai.com/blog/file-upload#error-handling Here is rough a template

async function handleServerAction(formData: FormData) {
try {
const actionResp = await serverAction(formData);
// check actionResp
} catch (error) {
// handle error
}
}