3

i want use firebase functions for a multipart upload.

This is my code:

https://pastebin.com/G0PzgtVg

const uploadVideo = Multer({
  storage: MulterStorage,
  limits: {
    fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
  },
  fileFilter: (req, file, cb) => {
    console.log('FILE', file);
    const mimetype = file.mimetype.split('/')[0];
    console.log('FILE', mimetype);

    if (mimetype !== 'video') {
      cb('Error: File is not an video');
    }

    return cb(null, true);
  },
}).single('blob');

But when i call the functions i have this error:

ERROR TypeError: Cannot read property 'filename' of undefined

I don't have this error in localhost.

SaroVin
  • 1,583
  • 3
  • 23
  • 46
  • 1
    Duplicate of https://stackoverflow.com/questions/47242340/how-to-upload-a-file-using-express-on-firebase-cloud-functions – SaroVin Dec 01 '17 at 11:08
  • 1
    Possible duplicate of [How to upload a file using express on firebase cloud functions](https://stackoverflow.com/questions/47242340/how-to-upload-a-file-using-express-on-firebase-cloud-functions) – Doug Stevenson Dec 01 '17 at 14:12

1 Answers1

0

Multer will not work with Firebase Functions. Instead, you can use express-multipart-file-parser.

Use the file parser:

const fileParser = require('express-multipart-file-parser')

router.use(fileParser);

// or

app.use(fileParser);

Destructure the file input:

async function upload(req, res, next) {
    try {

        const {
            fieldname,
            originalname,
            encoding,
            mimetype,
            buffer,
        } = req.files[0]

        console.log('fieldname:', fieldname);
        console.log('originalname:', originalname);
        console.log('encoding:', encoding);
        console.log('mimetype:', mimetype);
        console.log('buffer:', buffer);

        if (typeof req.files[0] != 'undefined') {
           // Do something
        }
      }
}
Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61