Skip to content

Instantly share code, notes, and snippets.

@andresgutgon
Last active February 16, 2022 16:39
Show Gist options
  • Save andresgutgon/9ba099f383658fd5f09e5a0897aa0743 to your computer and use it in GitHub Desktop.
Save andresgutgon/9ba099f383658fd5f09e5a0897aa0743 to your computer and use it in GitHub Desktop.
API of Remix createfileUploadHandler to handle form errors
// Documentation:
// https://remix.run/docs/en/v1/api/remix#unstable_createfileuploadhandler
const uploadHandler = unstable_createFileUploadHandler({
maxFileSize: 5_000_000,
file: ({ filename }) => filename
})
const fakeDb = createFakeDb()
export const action: ActionFunction = async ({ request }) => {
const formData = await unstable_parseMultipartFormData(
request,
uploadHandler
);
// Pseudo code
const avatarUrl = formData.get("avatar");
const name = formData.get("name")
const email = formData.get("email")
// My question:
// Image `fakeDb.users.create` returns an error because
// email doesn't have an email format or `name` is empty.
//
// At this point we already uploaded `avatar` file to our server / s3 bucket?
const results = await fakeDb.users.create({ name, email, avatarUrl })
if (result.success) {
return { errors: result.errors }
}
return result.data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment