Last active
February 16, 2022 16:39
-
-
Save andresgutgon/9ba099f383658fd5f09e5a0897aa0743 to your computer and use it in GitHub Desktop.
API of Remix createfileUploadHandler to handle form errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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