Error: Image Updating using Server Actions- Error: Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported. #5081
Unanswered
noushadkhalid
asked this question in
Questions & Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
`'use client'
import { LuSave, LuUndo } from "react-icons/lu";
import { editCategory } from "@/actions";
import { useState } from 'react';
import Image from "next/image";
import {useFormState } from 'react-dom';
const EditCategoryForm = ({category}) => {
const [name, setName] = useState(category.name);
const [filesBuffer, setFilesBuffer] = useState("");
const handleFileChange = async (event) => {
event.preventDefault();
};
const handleNameChange = (event) => {
setName(event.target.value);
// console.log('name changed')
};
console.log('filesBuffer collected(ArrayBuffer):', filesBuffer);
const editCategoryAction = editCategory.bind(null, category.id, name, filesBuffer);
<>
{category && (
)
}
</>
)
}
export default EditCategoryForm
// server actions
"use server"
import {redirect} from 'next/navigation'
import cloudinary from '@/config/cloudinary';
export async function editCategory(id, name, filesBuffer) {
// Directly use the imageBuffer as the upload data
const imageFileBuffer = filesBuffer;
let imageUrl;
try {
const uploadResult = await new Promise((resolve, reject) => {
cloudinary.uploader.upload_stream({ folder: 'malabar' }, (err, result) => {
if (err) reject(err);
else resolve(result);
}).end(imageFileBuffer);
});
} catch (err) {
console.error(err);
return;
}
console.log("Upload successful. Image URL:", imageUrl);
}`
Beta Was this translation helpful? Give feedback.
All reactions