TheNewGuy TheNewGuy - 7 days ago 6
Javascript Question

Cloudinary File Upload Error

I'm writing a REST API on using Express.js. The API needs to accept a video file from the client and upload it to cloudinary. When I use the api to return the file back to the client (as a test) everything works perfectly. When I then try to upload the same file to cloudinary I get an error. the error says:

"file.match is not a function"


I'm not sure what file.match even is or why it is giving me a problem. If anyone else has had this issue how did you solve it?
Below is the code that is giving me issues:

app.js

var express = require('express');
var formidable = require('express-formidable');
var app = express();

app.use(formidable());

var routes = require('./routes');
app.use('/routes', routes);

var port = process.env.PORT || 3000;

app.listen(port, function() {
console.log('Express server is listening on port ' + port);
});


routes.js

var express = require('express');
var cloudinary = require('../cloudinary.js').cloudinary;
var router = express.Router();

router.post('/upload', function(req, res, next) {
cloudinary.uploader.upload(req.files, function(result) {
console.log(result);
});
});

module.exports = router;


cloudinary.js

var cloudinary = require('cloudinary');

cloudinary.config({
cloud_name: 'name',
api_key: 'key',
api_secret: 'secret'
});

module.exports.cloudinary = cloudinary;

Answer

I was able to solve the issue. It was not a problem on Cloudinary's end. The key was to only send the location of the file.

WORKING routes.js

var express = require('express');
var cloudinary = require('../cloudinary.js').cloudinary;
var router = express.Router();

router.post('/upload', function(req, res, next) {
  var fileGettingUploaded = req.files.fileToUpload.path;

  cloudinary.uploader.upload(fileGettingUploaded, function(result) {
    console.log(result);
  });
});

module.exports = router;