Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read properties of undefined (reading 'secure_url') When i uploading image to cloudinary as const response = await uploadFileToCloudinary(file, "Mayank"); console.log(response); Then the response is printed as undefined and the error is provided #671

Open
Mayank141004 opened this issue Jul 17, 2024 · 4 comments

Comments

@Mayank141004
Copy link

No description provided.

@Mayank141004 Mayank141004 changed the title TypeError: Cannot read properties of undefined (reading 'secure_url') TypeError: Cannot read properties of undefined (reading 'secure_url') When i uploading image to cloudinary as const response = await uploadFileToCloudinary(file, "Mayank"); console.log(response); Then the response is printed as undefined and the error is provided Jul 17, 2024
@Mayank141004
Copy link
Author

const File = require("../model/File");
const cloudinary = require("cloudinary").v2;
const mongoose = require("mongoose");

//local file upload
exports.localFileUpload = async (req,res) => {
try{
const file = req.files.file;
console.log("file aagyi: " , file)

    let path = __dirname +  "/files/" + Date.now() + `.${file.name.split(".")[1]}`    //jo bhi current working directory hogi usko __dirname show karta hai
    
    console.log("Path->" , path);
    file.mv(path, (error) =>{
        console.log(error);
    });

    res.json({
        success:true,
        message:"Local file Uploaded successfully",
    })
}
catch(error){
    console.log("Not able to upload file");
    console.log(error);
}

}

function isFileTypeSupported(type, supportedTypes){
return supportedTypes.includes(type);
}

async function uploadFileToCloudinary(file,folder, quality){
const options = {folder};
if(quality){
options.quality = quality;
}
console.log("temp file pate is : " , file.tempFilePath);
options.resource_type = "auto";
await cloudinary.uploader.upload(file.tempFilePath, options);
}

exports.imageUpload = async(req,res) => {
try{
//data fetch
const {name, tags, email} = req.body;
console.log(name,tags,email);

    const file = req.files.imageFile;

    //validation
    const supportedTypes = ["jpg","jpeg","png"];
    const fileType =  file.name.split(".")[1].toLowerCase();
    console.log("fileType: " + fileType);

    if(!isFileTypeSupported(fileType,supportedTypes)) {
        return res.status(400).json({
            success:false,
            message:"file format not supported",
        })
    }

    const response = await uploadFileToCloudinary(file, "Mayank");
    console.log(response);

    const fileData = await File.create({
        name,
        tags,
        email,
        imageURL: response.secure_url,
    });

    res.json({
        success:true,
        // imageUrl:response.secure_url,
        message:"Image Successfully Uploaded",
    })

@skalahasti-cloudinary
Copy link

Hi @Mayank141004

console.log(response) when used with await can be return as a callback right? Can you please try the same?
const uploadResponse = await uploadFileToCloudinary(file, "Mayank", function(error,result){console.log(respone);});

Thanks,
Sree

@14Mayank2004
Copy link

{
message: 'Invalid transformation component - result){console.log(response);}',
name: 'Error',
http_code: 400
}

if i am doing the above said then i am getting this error

@wissam-khalili
Copy link

Hi @14Mayank2004 ,

Ensure that the uploadFileToCloudinary function returns the response from Cloudinary. Currently, the function does not return anything. Modify the function to return the upload result:
async function uploadFileToCloudinary(file, folder, quality) { const options = { folder }; if (quality) { options.quality = quality; } console.log("temp file path is: ", file.tempFilePath); options.resource_type = "auto"; const uploadResult = await cloudinary.uploader.upload(file.tempFilePath, options); return uploadResult; }

Thanks,
Wissam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants