TypeError: This stream has already been locked for exclusive reading by another reader #7660
-
I want to upload file after post created in database and then upload file in folder name is post.id but I get this error this is my source code export const addPost = async (request: Request ) => {
const formData = await request.formData()
const searchTagStr: string = formData.get('searchTag');
const searchTag = searchTagStr.split(',');
const post = await prisma.post.create({
data: {
title: formData.get('title'),
description: formData.get('description'),
content: formData.get('content'),
author: formData.get('author'),
keyword: formData.get('keyword'),
searchTag: searchTag,
},
});
const s3UploaderHandler: UploadHandler = async ({ filename, data, contentType }) => {
const customFileName = 'blog/'+ post.id + '/' + filename;
return await uploadStreamToS3(data, customFileName, contentType);
};
const fileData = await unstable_parseMultipartFormData(request, s3UploaderHandler);
const fileName = fileData.get('assets') as File;
const titleImage = fileData.get('titleImage') as File;
const updatedPost = prisma.post.update({
where: { id: post.id },
data: { image: `https://pertadex-storage.storage.iran.liara.space/${titleImage}` },
});
return updatedPost;
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
You can only read the body of a request once, and you're calling I recommend you to just use Alternatively, you can do |
Beta Was this translation helpful? Give feedback.
You can only read the body of a request once, and you're calling
request.formData()
and later usingunstable_parseMultipartFormData
which also reads the body.I recommend you to just use
unstable_parseMultipartFormData
to get the FormData and the files and do theprisma.post.create
after that.Alternatively, you can do
request.clone().formData()
to clone the request first, but since it comes with files it may consume too much memory.