UploadThing Middleware failed to run
saurabhbhise16 opened this issue · comments
- ErrorLog:
event compiled client and server successfully in 318 ms (1062 modules)
[UT] UploadThing dev server is now running!
[UT] UploadThing dev server is now running!
[UT] UploadThing dev server is now running!
[UT] middleware failed to run
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11413:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: ConnectTimeoutError: Connect Timeout Error
at onConnectTimeout (C:\Users\Brady\Desktop\threads\node_modules\next\dist\compiled\undici\index.js:1:82152)
at C:\Users\Brady\Desktop\threads\node_modules\next\dist\compiled\undici\index.js:1:81644
at Immediate._onImmediate (C:\Users\Brady\Desktop\threads\node_modules\next\dist\compiled\undici\index.js:1:82023)
at process.processImmediate (node:internal/timers:476:21)
at process.callbackTrampoline (node:internal/async_hooks:130:17) {
code: 'UND_ERR_CONNECT_TIMEOUT'
}
}
connected to MonogDB
import { currentUser } from "@clerk/nextjs";
import { createUploadthing, type FileRouter } from "uploadthing/next";
const f = createUploadthing();
const getUser = async () => await currentUser();
export const ourFileRouter = {
// Define as many FileRoutes as you like, each with a unique routeSlug
media: f({ image: { maxFileSize: "4MB", maxFileCount: 1 } })
// Set permissions and file types for this FileRoute
.middleware(async (req) => {
// This code runs on your server before upload
const user = await getUser();
// If you throw, the user will not be able to upload
if (!user) throw new Error("Unauthorized");
// Whatever is returned here is accessible in onUploadComplete as `metadata`
return { userId: user.id };
})
.onUploadComplete(async ({ metadata, file }) => {
// This code RUNS ON YOUR SERVER after upload
console.log("Upload complete for userId:", metadata.userId);
console.log("file url", file.url);
}),
} satisfies FileRouter;
export type OurFileRouter = typeof ourFileRouter;
I am at halfway and realised that onboarding page is showing this error and thisis my third trial
Please let me know if you need more details
hello, I think you should use const {user} = await getUser(); instead of const user = await getUser();
Let me know if it helps you.
@saurabhbhise16 Have you properly imported the uploadthing secret keys in the .env file ?
Also have a check that if you have exported the GET & POST methods in the route.ts file.
@Reedam-Ranjan @Ahmad-Dorri thank you for interest, sure I will make changes and let you guys know