Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
Uploading images to cloudinary in MERN Stack gives a 500 error

So I have been trying to upload an image to cloudinary from by react front end. but when I upload the image it gives me 'undefined' alert in client side. So I used the chrome console and found out that the error indeed is 'Failed to load resource: the server responded with a status of 500' in the http://localhost:3000/api/upload. i checked my backend and this error is populating from the catch in the router post function.

this is the upload.js file'

const uprouter = express.Router()
import cloudinary from 'cloudinary';
//import auth from '../middleware/auth.js';
//import authAdmin from '../middleware/authAdmin.js';
import fs from 'fs';

// we will upload image on cloudinary
cloudinary.config({
    cloud_name: process.env.CLOUD_NAME,
    api_key: process.env.CLOUD_API_KEY,
    api_secret: process.env.CLOUD_API_SECRET
})

// Upload image only admin can use
uprouter.post('/upload', (req, res) =>{
    try {
        if(!req.files || Object.keys(req.files).length === 0)
            return res.status(400).json({msg: 'No files were uploaded.'})
        
        const file = req.files.file
        if(file.size > 1024*1024) {
            removeTmp(file.tempFilePath)
            return res.status(400).json({msg: "Size too large"})
        }

        if(file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png'){
            removeTmp(file.tempFilePath)
            return res.status(400).json({msg: "File format is incorrect."})
        }
        if(file.size === undefined) {
            console.log("myProperty value is the special value `undefined`")
          }

        cloudinary.v2.uploader.upload(file.tempFilePath, {folder: "test"}, async(err, result)=>{
            if(err) throw err;
            removeTmp(file.tempFilePath)
            res.json({public_id: result.public_id, url: result.secure_url})
        })


    } catch (err) {
        console.log("f")
        return res.status(500).json({msg: err.message})
    }
})

what seems to be the issue?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!