Vercel Blob & Server Actions
Unanswered
Taiwan Dog posted this in #help-forum
Taiwan DogOP
I am trying to use vercel blob to upload an image inside a form server action. The server action works only when the await auth function isn't there or behind the fetch request. Any help is greatly appreciated. This is just an example, I am trying to add this to a form with other data.
_action.ts
"use server";
import { auth } from "@/lib/auth";
import { type PutBlobResult } from "@vercel/blob";
export async function UploadImage(fd: FormData) {
const session = await auth();
const userId = session?.user.id;
console.log("userId", userId);
if (!userId) {
return {
errors: {
auth: ["You must be logged in to submit a map"],
},
message: "You must be logged in to submit a map",
};
}
const file = fd.get("file") as File;
const response = await fetch(
`http://localhost:5500/api/image/upload?filename=${file.name}`,
{
method: "POST",
body: file,
},
);
const newBlob = (await response.json()) as PutBlobResult;
console.log("newBlob", newBlob);
}
api/image/upload/route.ts
import { put } from "@vercel/blob";
import { NextResponse } from "next/server";
export async function POST(request: Request): Promise<NextResponse> {
const { searchParams } = new URL(request.url);
const filename = searchParams.get("filename");
if (!filename) {
return NextResponse.json(
{ error: "filename is required" },
{ status: 400 },
);
}
if (!request.body) {
return NextResponse.json({ error: "body is required" }, { status: 400 });
}
const blob = await put(filename, request.body, {
access: "public",
});
return NextResponse.json(blob);
}11 Replies
Peterbald
Hey there?
What does it mean "The server action works only when the await auth function isn't there or behind the fetch request"?
According your code, upload fetch function can works only when there is user session.
According your code, upload fetch function can works only when there is user session.
@Peterbald What does it mean "The server action works only when the await auth function isn't there or behind the fetch request"?
According your code, upload fetch function can works only when there is user session.
Taiwan DogOP
For some reason the fetch request doesn't successfully fire when the await auth function triggers. Even when the session and userId are valid. I get a connection timeout with this
" cause: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read'
}
} "
When I comment out the await auth function it works perfectly
" cause: Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20)
at TLSWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
errno: -104,
code: 'ECONNRESET',
syscall: 'read'
}
} "
When I comment out the await auth function it works perfectly
Peterbald
what kind of session are you using?
Supabase or Firebase?
Taiwan DogOP
Nextauth
NextAuth with Drizzle Adapter and Planetscale
Peterbald
So you don't want to use await in this function?
Next.js is working on asynchronose programing method. It is because Next.js is based on Node.js
So you should use await in this function
By doing so it can work as sychronose