#i need some help about error upload image on deploy

2 messages · Page 1 of 1 (latest)

eager blade
#

in local it work fine and can upload to cloudinary

language :nextjs and use server action

I have this Error message after deploy :

message: 'Server return invalid JSON response. Status Code 500. SyntaxError: Unexpected token < in JSON at position 0',
name: 'Error',
http_code: 500

This is my code

async function uploadphotoToCloud(formData) {
  try {
    const files = formData.getAll("files");
    const file = files[0]; // Only process the first file
    const data = await file.arrayBuffer();
    const buffer = Buffer.from(data);
    const name = uuidv4();
    const ext = file.type.split("/")[1];

    return new Promise((resolve, reject) => {
      cloudinary.v2.uploader
        .upload_stream(
          {
            resource_type: "image", // Ensure resource type is 'image'
            public_id: `uploadfrom_nextjs/${name}`,
            format: ext, // Specify the image format
          },
          (error, result) => {
            if (error) {
              console.error("Upload to Cloudinary error:", error);
              reject(error);
            } else {
              console.log("Upload result:", result);

              resolve([result]); // Return an array with a single object for consistency
            }
          }
        )
        .end(buffer);
    });
  } catch (error) {
    console.error(
      "uploadphotoToCloud error response:",
      error.response ? error.response.data : error
    );

    return { error: error.message };
  }
}

export async function uploadPhoto(formData, userId) {
  try {
    // Upload to Cloudinary
    const photos = await uploadphotoToCloud(formData);

    // Check for errors from Cloudinary
    if (photos.error) throw new Error(photos.error);

    // Update photo URL in MongoDB
    const newPhotos = photos.map((photo) => {
      const newPhoto = { image: photo.secure_url };
      console.log("newphoto:", newPhoto);
      return newPhoto;
    });

    const user = await User.findByIdAndUpdate(
      userId,
      { image: newPhotos[0].image },
      { new: true }
    );
    revalidatePath("/");
    if (!user) {
      return { error: "User not found" };
    }

    return { msg: "เพิ่มรูปภาพสำเร็จ", image: newPhotos };
  } catch (error) {
    console.error(
      "uploadPhoto error response:",
      error.response ? error.response.data : error
    );
    return { error: error.message };
  }
}
eager blade
#

i need some help about error upload image on deploy