cloudinary / cloudinary_npm

Cloudinary NPM for node.js integration
629 stars 323 forks source link

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 #672

Closed 14Mayank2004 closed 4 months ago

14Mayank2004 commented 4 months ago

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",
    })
igy commented 4 months ago

Duplicate of #671