uploadthing replacing name and adding customId in middleware fails to run

This middleware does not run, despite this being close to what is provided in the documentation for changing file names and adding customIds (https://docs.uploadthing.com/file-routes#middleware). Logging the error provided by useUploadThing()'s onUploadError gives UploadThingError: Failed to run middleware... Caused by: Object { … }
.middleware(async ({ req, files }) => {
// This code runs on your server before upload
const { user } = await validateRequest();

// If you throw, the user will not be able to upload
if (!user) throw new UploadThingError("Unauthorized");

// Override file name and add customId
const fileOverrides = files.map((file) => {
const newName = uniqueNamesGenerator({
dictionaries: [adjectives, animals, colors],
length: 4,
});
const uuid = crypto.randomUUID();
return { ...file, name: newName, customId: uuid };
});

// Whatever is returned here is accessible in onUploadComplete as `metadata`
// Return userId to be used in onUploadComplete
return { userId: user.id, [UTFiles]: fileOverrides };
// return { userId: user.id };
})
.middleware(async ({ req, files }) => {
// This code runs on your server before upload
const { user } = await validateRequest();

// If you throw, the user will not be able to upload
if (!user) throw new UploadThingError("Unauthorized");

// Override file name and add customId
const fileOverrides = files.map((file) => {
const newName = uniqueNamesGenerator({
dictionaries: [adjectives, animals, colors],
length: 4,
});
const uuid = crypto.randomUUID();
return { ...file, name: newName, customId: uuid };
});

// Whatever is returned here is accessible in onUploadComplete as `metadata`
// Return userId to be used in onUploadComplete
return { userId: user.id, [UTFiles]: fileOverrides };
// return { userId: user.id };
})
File Routes - UploadThing Docs
File Routes is a core concept of UploadThing that defines what your users can upload
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server