formData
Answered
Polar bear posted this in #help-forum
Polar bearOP
I'm using formData.get(), but my images only show 1 image, but sometimes it shows multiple images. I wanted to formData.get the images and turn them into base64 images so I can insert into supabase database.
so 2 questions.
1. Why do I sometimes lack consistency, after uploading multiple images, only get 1 image or multiple?
2. What is the best way in Next.JS to encode to base64 so I can upload the form data of the images to supabase?
current code:
export async function addProduct(formData) {
const title = formData.get("title")
const description = formData.get("description")
const shipping = formData.get("shipping")
const quantity = formData.get("quantity")
const condition = formData.get("condition")
const price = formData.get("price")
const images = formData.get("images")
console.log(" the images",images)
console.log("length image: ", images.length)
const cookieStore = cookies()
const supabase = createServerComponentClient( {cookies: () => cookieStore})
const {data: {session}} = await supabase.auth.getSession()
const user = session?.user
if(!user) {
console.error("User isn't authenticated within product server action")
return
}
const {data, error} = await supabase
.from('products')
.insert([
{
title,
description,
shipping,
quantity,
condition,
price,
user_id: user.id
}
])
if (error) {
console.error("Error inserting data")
return
}
revalidatePath("/dashboard")
revalidatePath("/dashboard/products")
revalidatePath("/products")
return {message: "Success"}
}
so 2 questions.
1. Why do I sometimes lack consistency, after uploading multiple images, only get 1 image or multiple?
2. What is the best way in Next.JS to encode to base64 so I can upload the form data of the images to supabase?
current code:
export async function addProduct(formData) {
const title = formData.get("title")
const description = formData.get("description")
const shipping = formData.get("shipping")
const quantity = formData.get("quantity")
const condition = formData.get("condition")
const price = formData.get("price")
const images = formData.get("images")
console.log(" the images",images)
console.log("length image: ", images.length)
const cookieStore = cookies()
const supabase = createServerComponentClient( {cookies: () => cookieStore})
const {data: {session}} = await supabase.auth.getSession()
const user = session?.user
if(!user) {
console.error("User isn't authenticated within product server action")
return
}
const {data, error} = await supabase
.from('products')
.insert([
{
title,
description,
shipping,
quantity,
condition,
price,
user_id: user.id
}
])
if (error) {
console.error("Error inserting data")
return
}
revalidatePath("/dashboard")
revalidatePath("/dashboard/products")
revalidatePath("/products")
return {message: "Success"}
}
8 Replies
Polar bearOP
I think I'm going to go with : npm i next-base64 .... to encode and decode base64 files
not sure if this is the best solution, but it seems like the most simple solution.
@Polar bear I think I'm going to go with : npm i next-base64 .... to encode and decode base64 files
is this what you want?
const images = formData.getAll("images")Answer
@Ray is this what you want?
`const images = formData.getAll("images")`
Polar bearOP
Oh thank you 😊 ! Merci
@Polar bear Oh thank you 😊 ! Merci
it does what you looking for?
@Ray it does what you looking for?
Polar bearOP
Im about to test it out, but im sure it’ll solve the issue.
@Polar bear Im about to test it out, but im sure it’ll solve the issue.
ok cool, please mark solution if it works for you
Polar bearOP
Will do