hiteshchoudhary / chai-backend

A video series on chai aur code youtube channel
4.68k stars 696 forks source link

Avatar File is Required issue #34

Open Talhaayubkhan opened 8 months ago

Talhaayubkhan commented 8 months ago

I checked and everything is working, and when I sent a request using Postman,  this avatar file issue occurred. How can I resolve this issue?

Screenshot 2023-12-23 220508
nirajchaurasiya commented 8 months ago

Hey, I think the condition you are writing for the avatar file is wrong.

Check the if condition once.

Additionally, you can provide me with the repo URL.

anishudupa commented 8 months ago

check for middleware once and check if you are accessing the file correctly (req.files not req.file) check here - https://github.com/expressjs/multer or else provide your source code repo link

Talhaayubkhan commented 8 months ago

Can you clone my repo? or did I send the link? because I checked everything but the avatar file is still throwing issue

nirajchaurasiya commented 8 months ago

Yeah, you can send us the repo URL.

Talhaayubkhan commented 8 months ago

https://github.com/Talhaayub1/backend.git this is the repo!!!!

nirajchaurasiya commented 8 months ago

I will update you after checking.

nirajchaurasiya commented 8 months ago

Can you provide me the .env.sample file with only names of variables?

Talhaayubkhan commented 8 months ago

i didn't create .env.sample, i just create .env

nirajchaurasiya commented 8 months ago

So, you have to create it or just provide me with a screenshot of the variable names.

Because I think I got your issue.

Talhaayubkhan commented 8 months ago
Screenshot 2023-12-24 191528

check this

nirajchaurasiya commented 8 months ago

Hmm, I see. Everything is fine. Can you check the keys of Cloudinary once? Because the problem comes with uploading the file to Cloudinary.

nirajchaurasiya commented 8 months ago

Yeah, just a silly mistake, man. Check this video👇

https://github.com/hiteshchoudhary/chai-backend/assets/116571542/4660e427-706b-4ec5-86cc-10a3e1161747

nirajchaurasiya commented 8 months ago

And, then you can see your api_secret.

Talhaayubkhan commented 8 months ago

got it man!! Thanks For this, resolve the issue 🙌❤️

nirajchaurasiya commented 8 months ago

Glad to help❤️

adityamehta242 commented 8 months ago

brother can u help me?? solve this problem Screenshot (75) Screenshot (76) Screenshot (78) Screenshot (79)

Divyanshu7001 commented 8 months ago

well..it is not a error actually...the users/register route is supposed to be a post route as we have written in our code..& due to that reason..the server cant get that route..cause it is actually supposed to be a POST method. Screenshot 2023-12-25 021446

now..if we change it to GET method...the error changes as we can see the change here Screenshot 2023-12-25 021649 Screenshot 2023-12-25 021724

The error changes as the users/register is now a GET method but no avatar has been given as input due to no frontend yet..and it gets a error of that portion.

hope this explanation helps you!!

adityamehta242 commented 8 months ago

thanks for your efforts . i do all this changes i cant fix the ploblem. i think i some line previously

itzmeimran commented 8 months ago

Here dont use GET method instead use POST method as you are supposed to send files. Hope this ll help you fix error

shobitkhatri05 commented 5 months ago

Hello Brother @nirajchaurasiya I also the face same issue avatar is required Can you clone my repo? or did I send the link? because I checked everything but the avatar file is still throwing issue

RAMraghuram commented 5 months ago

Avatar is required issues

The issue is within the cloudinary.js file. When we are unlinking the local file path before sending the response, we should first check if the local file exists or not. As it is happening asynchronously, there is a chance that it is unlinking before all other tasks are completed. The best option would be to check if localFilePath exists, and then unlink it using the following code:

fs.existsSync(localFilePath) && fs.unlinkSync(localFilePath);

This is the final version:

import { v2 as cloudinary } from "cloudinary";
import fs from "fs";

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});

const uploadCloudinary = async (localFilePath) => {
  try {
    if (!localFilePath) return null;
    console.log("localFilePath", localFilePath);
    // upload file to cloudinary
    const response = await cloudinary.uploader.upload(localFilePath, {
      resource_type: "auto",
    });
    // file has been uploaded
    // console.log("File uploaded to cloudinary", response.url);
    fs.existsSync(localFilePath) && fs.unlinkSync(localFilePath);
    // console.log("response", response);
    return response;
  } catch (error) {
    fs.existsSync(localFilePath) && fs.unlinkSync(localFilePath); // remove file from local storage if cloudinary upload fails
    return null;
  }
};

export { uploadCloudinary };
akashsahaks commented 5 months ago

Great to hear this, I will verify this!!

snehitshh commented 3 months ago

i am having the same issue can someone please help, i tried the above methods none of them worked :(

harshgarg007 commented 2 months ago

Avatar is required issues

The issue is within the cloudinary.js file. When we are unlinking the local file path before sending the response, we should first check if the local file exists or not. As it is happening asynchronously, there is a chance that it is unlinking before all other tasks are completed. The best option would be to check if localFilePath exists, and then unlink it using the following code:

fs.existsSync(localFilePath) && fs.unlinkSync(localFilePath);

This is the final version:

import { v2 as cloudinary } from "cloudinary";
import fs from "fs";

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});

const uploadCloudinary = async (localFilePath) => {
  try {
    if (!localFilePath) return null;
    console.log("localFilePath", localFilePath);
    // upload file to cloudinary
    const response = await cloudinary.uploader.upload(localFilePath, {
      resource_type: "auto",
    });
    // file has been uploaded
    // console.log("File uploaded to cloudinary", response.url);
    fs.existsSync(localFilePath) && fs.unlinkSync(localFilePath);
    // console.log("response", response);
    return response;
  } catch (error) {
    fs.existsSync(localFilePath) && fs.unlinkSync(localFilePath); // remove file from local storage if cloudinary upload fails
    return null;
  }
};

export { uploadCloudinary };

this is not working bro 😭😭 error in vs code terminal :- MongoDB Connected !! DB HOST: localhost 😎 Server listening on 👉 8000 email: h1@.com public\temp\pic.jpeg Error: Avatar is required at file:///D:/chaibackend/src/controllers/user.controller.js:47:11 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

AmarNarayanDwivedi commented 2 months ago

i am stuck from a month in this issue please help me guys thsi is my repo link : https://github.com/AmarNarayanDwivedi/Backend-Project/tree/main Screenshot 2024-06-25 161203

harshgarg007 commented 2 months ago

const avatarLocalPath = req.files?.avatar ?.[0]?.path; Use this

AmarNarayanDwivedi commented 2 months ago

same error no effect

Talhaayubkhan commented 2 months ago

can you copy necessary things from cloudinary???? means what we store in .env file to send avatar to cloudinary?? try to update those

AmarNarayanDwivedi commented 2 months ago

i didn't understand what you are telling about

is you are suggesting to change Cloudinary keys

Talhaayubkhan commented 2 months ago

CLOUDINARY_CLOUD_NAME= dqypjs4tl CLOUDINARY_API_KEY= 225719471113895 CLOUDINARY_API_SECRET= hxjkdbdBSSHk68jdhh3dd35h4kj rerecheck these keys

and make sure you correctly write multer middleware

Sagar-1103 commented 2 months ago

the problem is we are not actually using the cloudinary.config part , to fix it make sure u put the config part inside the function like below :

cloudinaryconfig

and if you are getting this error - TypeError: Cannot read properties of undefined (reading '0')
then just use this in your user controller

const avatarLocalPath = await req?.files?.avatar?.[0]?.path; const coverImageLocalPath = await req?.files?.coverImage?.[0]?.path;

we are getting this error because when a file is not uploaded then avatar[0] doesnt make any sense