Cloudinary error: Missing required parameter - file. Prisma/MongoDB/Insomnia

29 Views Asked by At

I am building my first fullstack twitter clone app, while following a tutorial, and so far I am facing lots of errors, but I was able to fix them so far my own. However, on this one I need some help, since it doesn't make a lot of sense for me.

I am currently trying to build the Tweet form (which gets the tweets from the respective author/user) and displays them. However, I am getting the following error while testing it with Insomnia: enter image description here

My index.post.js file (to post tweets):

import formidable from "formidable";
import { createTweet } from "~/server/db/tweets";
import { tweetTransformer } from "~/server/transformers/tweet.js";
import { createMediaFile } from "../../../db/mediaFiles.js";
import { uploadToCloudinary } from "../../../utils/cloudinary.js";

export default defineEventHandler(async (event) => {
  const form = formidable({});

  const response = await new Promise((resolve, reject) => {
    form.parse(event.node.req, (err, fields, files) => {
      if (err) {
        reject(err);
      }
      resolve({ fields, files });
    });
  });

  const { fields, files } = response;

  const userId = event.context?.auth?.user?.id;

  const tweetData = {
    text: String(fields.text),
    authorId: userId,
  };

  const tweet = await createTweet(tweetData);

  const filePromises = Object.keys(files).map(async (key) => {
    const file = files[key];

    const response = await uploadToCloudinary(file.filepath);

    console.log(response);

    return createMediaFile({
      url: "",
      providerPublicId: "random_id",
      userId: userId,
      tweetId: tweet.id,
    });
  });

  await Promise.all(filePromises);

  return {
    // tweet: tweetTransformer(tweet),
    files,
  };
});

My tweets.js file:

import { prisma } from ".";

export const createTweet = (tweetData) => {
  return prisma.tweet.create({
    data: tweetData,
  });
};

My cloudinary.js file:

import { v2 as _cloudinary } from "cloudinary";

const cloudinary = () => {
  const config = useRuntimeConfig();

  _cloudinary.config({
    cloud_name: config.cloudinaryCloudName,
    api_key: config.cloudinaryApiKey,
    api_secret: config.cloudinaryApiSecret,
  });

  return _cloudinary;
};

export const uploadToCloudinary = (image) => {
  return new Promise((resolve, reject) => {
    console.log(image);
    cloudinary().uploader.upload(image, (error, data) => {
      if (error) {
        reject(error);
      }
      resolve(data);
    });
  });
};

Maybe there is something wrong in the configuration of the cloudinary file? Any help would be appreciated!

Tried getting help from chatGPT, already saw a couple forums that had the exact same problem and tried their solutions, but no luck so far. Maybe my error is more specific?

1

There are 1 best solutions below

0
Wissam Khalili On

The error message you’re encountering is related to a missing required parameter when using Cloudinary. Here are points to review:

  1. Ensure that the configuration parameters (cloud_name, api_key, and api_secret) are correctly set.
  2. Make sure that the image variable contains the correct path or URL to the image you want to upload. If it’s a local file, ensure that the path is valid and accessible.
  3. The console.log(image) statement is helpful for debugging. Verify that it displays the correct image path or URL.