Azoulay Jason
Azoulay Jason

Reputation: 2989

Multer upload is not a function

I'm using Multer to make an upload file system. I followed the instructions of the Github page but it's not working.

const express= require('express');
const app = express();
const multer = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/uploads');
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now());
  }
});

const upload = multer({ storage: storage });


app.post('/editPhoto',upload.single('avatar'),function(req,res,next){
  upload(req,res,function(err){   
      if(err){
        res.json({success:false,message:err});
        
      }
      else{
        res.json({success:true,message:"Photo was updated !"});
      } 

  });
});

I get TypeError: upload is not a function

What am I doing wrong ?

EDIT

I did as you said and as the doc says.

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads');
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now());
  }
});

app.post('/editPhoto',function(req,res,next){

  var upload = multer({ storage:storage}).single('userPhoto');
  upload(req,res,function(err){   
      console.log(req.file);
      if(err){
        res.json({success:false,message:err});
      }
      else{
        res.json({success:true,message:"Photo was updated !"});
      } 
  });
});

req.file is undefined

and when i try like this

var upload = multer({ storage:storage});

app.post('/editPhoto',function(req,res,next){
  upload(req,res,function(err){   
      console.log(req.file);
      if(err){
        res.json({success:false,message:err});
      }
      else{
        res.json({success:true,message:"Photo was updated !"});
      } 
  });
});

I get upload is not a function

Upvotes: 14

Views: 26574

Answers (3)

JAYAKUMAR MANICKAM
JAYAKUMAR MANICKAM

Reputation: 1

var multer = require('multer');
var upload = multer({ dest: './temp/hh' })

var cpUpload = upload.fields([{ name: 'audio_file', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/v1/index/uploads', cpUpload, function (req, res, next) {

  console.log(req.files['audio_file'][0]);
   console.log(req.files['gallery']);
 
})

Upvotes: -2

Nhan Cao
Nhan Cao

Reputation: 2515

// Multer upload config
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/tmp/')
    },
    filename: function (req, file, cb) {
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
        cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
    }
})
const upload = multer({storage: storage});
app.post('/uploadPhoto', (req, res, next) => {
    upload.single('filename_here')(req, res, function (err) {
        if (err) {
            // A Multer error occurred when uploading.
            res.json({msg: err.message})
        } else {
            // Everything went fine.
            // req.file
            // {
            //   fieldname: 'filename_here',
            //   originalname: 'nhancv_dep_trai.png',
            //   encoding: '7bit',
            //   mimetype: 'image/png',
            //   destination: '/tmp/',
            //   filename: 'filename_here-1607694392023-220481630',
            //   path: '/tmp/filename_here-1607694392023-220481630',
            //   size: 5907
            // }
            const file = req.file;
            console.log(file);

            // Delete tmp
            try {
                // fs.unlinkSync(file.path);
            } catch (e) {
                // Ignore
                console.error(e);
            }
            res.json({msg: 'ok'});
        }
    });
})

Test

curl --location --request POST '<host>:<port>/uploadPhoto' \
--form 'filename_here=@"/Users/nhancv/Desktop/nhancv_dep_trai.png"'

Upvotes: 1

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

As @Aabid told in The comments you will not need to use both, the multer middleware and upload in the controller.

You can use:

app.post('/editPhoto', upload.single('avatar'), (req, res, next) => {
   // here in the req.file you will have the uploaded avatar file
})

Or you can use:

app.post('/editPhoto', (req, res, next) => {
  upload(req, res, function (err) {
    if (err) {
      // This is a good practice when you want to handle your errors differently

      return
    }

    // Everything went fine 
  })
})

So you use one of the 2 methods, not both at the same time.

Upvotes: 9

Related Questions