firebase-functions: Multipart upload is broken

Version info

**firebase-functions: 0.7.3

**firebase-tools: 3.15.4

**firebase-admin: 5.5.1

Hi, i want use multer with my firebase functions. If i use the function with the emulator, it works but if i deploy, the function return this error: ERROR TypeError: Cannot read property 'filename' of undefined

This is my code => https://pastebin.com/G0PzgtVg

It’s possible upload a file with the firebase functions?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 19 (1 by maintainers)

Commits related to this issue

Most upvoted comments

The guide tells you to use a low level library for multipart data processing, which is not ideal if you already are using some web framework. If you are using express for your http functions, you can use a forked version of multer middleware for processing multipart/form-data on cloud functions.

Here’s what you can do.

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

const SIZE_LIMIT = 10 * 1024 * 1024 // 10MB
const app = express()

const multipartFormDataParser = multer({
  storage: multer.memoryStorage(),
  // increase size limit if needed
  limits: {fieldSize: SIZE_LIMIT},
  // support firebase cloud functions
  // the multipart form-data request object is pre-processed by the cloud functions
  // currently the `multer` library doesn't natively support this behaviour
  // as such, a custom fork is maintained to enable this by adding `startProcessing`
  // https://github.com/emadalam/multer
  startProcessing(req, busboy) {
    req.rawBody ? busboy.end(req.rawBody) : req.pipe(busboy)
  },
})

app.post('/some_route', multipartFormDataParser.any(), function (req, res, next) {
  // req.files is array of uploaded files
  // req.body will contain the text fields
})

@sarovin Thanks for the report and glad you found the answer on Stackoverflow, presently that’s the best way to deal with your issue.

… so is there any plan to fix this?

I get that this is in beta, but you’re really going to just silently break something, not document it and not even keep the bug report open?

Thanks @emadalam I spent a tonne of hours debugging. I set up demo project on node.js and my code worked with multer but break in cloud functions.

For the next person that stumbles across this issue: the official docs have some code: https://cloud.google.com/functions/docs/writing/http#multipart_data

Also interested in this. Any update?

I’m so glad this is closed with no solution. Guess I just won’t post data to a firebase function… 🤭

@ARPNetBR What does not work? The original multer library still doesn’t support this. If you want to use it, use the forked version in your npm dependency by pointing to the GitHub url https://github.com/emadalam/multer and then use the code sample as provided.

A few notes:

  • make sure to clean up your node modules for local testing
  • the version is old and hasn’t been updated to match the latest multer or busboy
  • I’ve been using this forked version on production forever without issues

Thanks @emadalam for anwering, I followed the steps, and now its working

@ARPNetBR What does not work? The original multer library still doesn’t support this. If you want to use it, use the forked version in your npm dependency by pointing to the GitHub url https://github.com/emadalam/multer and then use the code sample as provided.

A few notes:

  • make sure to clean up your node modules for local testing
  • the version is old and hasn’t been updated to match the latest multer or busboy
  • I’ve been using this forked version on production forever without issues

Did anybody looked at this in the past year?