Open Talhaayubkhan opened 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.
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
Can you clone my repo? or did I send the link? because I checked everything but the avatar file is still throwing issue
Yeah, you can send us the repo URL.
https://github.com/Talhaayub1/backend.git this is the repo!!!!
I will update you after checking.
Can you provide me the .env.sample file with only names of variables?
i didn't create .env.sample, i just create .env
So, you have to create it or just provide me with a screenshot of the variable names.
Because I think I got your issue.
check this
Hmm, I see. Everything is fine. Can you check the keys of Cloudinary once? Because the problem comes with uploading the file to Cloudinary.
Yeah, just a silly mistake, man. Check this video👇
And, then you can see your api_secret.
got it man!! Thanks For this, resolve the issue 🙌❤️
Glad to help❤️
brother can u help me?? solve this problem
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.
now..if we change it to GET method...the error changes as we can see the change here
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!!
thanks for your efforts . i do all this changes i cant fix the ploblem. i think i some line previously
Here dont use GET method instead use POST method as you are supposed to send files. Hope this ll help you fix error
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
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 };
Great to hear this, I will verify this!!
i am having the same issue can someone please help, i tried the above methods none of them worked :(
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 iflocalFilePath
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)
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
const avatarLocalPath = req.files?.avatar ?.[0]?.path; Use this
same error no effect
can you copy necessary things from cloudinary???? means what we store in .env file to send avatar to cloudinary?? try to update those
i didn't understand what you are telling about
is you are suggesting to change Cloudinary keys
CLOUDINARY_CLOUD_NAME= dqypjs4tl CLOUDINARY_API_KEY= 225719471113895 CLOUDINARY_API_SECRET= hxjkdbdBSSHk68jdhh3dd35h4kj rerecheck these keys
and make sure you correctly write multer middleware
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 :
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
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?