0

This is the first time I'm deploying a NodeJS/ExpressJS API to Firebase functions and I'm getting this error:

Error: Unexpected end of form
   at Multipart._final (/.../functions/node_modules/busboy/lib/types/multipart.js:588:17)
   at callFinal (node:internal/streams/writable:696:27)
   at prefinish (node:internal/streams/writable:725:7)
   at finishMaybe (node:internal/streams/writable:735:5)
   at Multipart.Writable.end (node:internal/streams/writable:633:5)
   at onend (node:internal/streams/readable:693:10)
   at processTicksAndRejections (node:internal/process/task_queues:78:11)

I'm trying to do is to send an image to the /upload end-point which takes the photo run it through mobilenet from Tensorflow and return the predictions array. My Code of index.js is:

const app = express();

const storage = multer.memoryStorage();

app.use(cors());

const upload = multer({ storage });

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//#region Mobile Net Endpoint
let model;

mobilenet.load().then(loadedModel => {
    model = loadedModel;
    console.info('MobileNet model loaded successfully');
}).catch(error => {
    console.error('Error loading MobileNet model:', error);
});

app.post('/upload', upload.single('file'), (req, res) => {
    // Handle the uploaded file here
    const file = req.file;

    if (!file) {
        return res.status(400).json({ message: 'No file uploaded' });
    } else if (file) {

        const readImage = () => {
            const imageBuffer = file.buffer;
            const tfimage = tfnode.node.decodeImage(imageBuffer);
            return tfimage;
        }

        const imageClassification = async () => {
            const image = readImage();
            const mobilenetModel = await mobilenet.load();
            const predictions = await mobilenetModel.classify(image);
            return res.json({ predictions });
            // console.log('Classification Results:', predictions);
        }

        imageClassification().then((pred) => {
            console.log("pred: ", pred);
        }).catch((error) => {
            console.log("error: ", error);
            return res.status(400).json({ message: error });
        })

    }
});
//#endregion

I don't want to store the image in firebase storage or others that is why there is no implementation of that. I wanted the image to stay in tmp folder while the predictions are being calculated by mobile net. It works on localhost where the image is stored in my hard drive so not sure what I'm doing wrong for firebase functions here.

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • 1
    Have you tried downgrading `multer` to either `1.4.2` or `1.4.3` based on this [Github issue link](https://github.com/expressjs/multer/issues/1144)? – Robert G Jun 27 '23 at 19:26
  • @RobertG looks like this might just solve the issue however I'm sending the file in the body with a `key: file` and value is the image.png I'm attaching on postman. When I `console.log(req.body)` I see `req.body: [Object: null prototype] {}` which basically returns `No file uploaded`. So basically the `req.file` is `undefined` which I'm not sure why. – Chaudhry Talha Jun 28 '23 at 04:50
  • I've noticed that this is still an ongoing issue and there are several workarounds that may work with your project. In case that those workarounds still won't work, you may file this as a bug by adding a reply through the Github link that I previously sent or through [Firebase Bug Report](https://firebase.google.com/support/troubleshooter/report/bugs). – Robert G Jun 28 '23 at 13:12

1 Answers1

2

There are several factors that may cause Error: Unexpected end of form at Multipart._final...:

  • There's an ongoing issue with Multer and some users suggested downgrading to either 1.4.2 or 1.4.3 as mentioned in this Github issue link.

  • Some also mentioned that they used body-parser middleware as mentioned in this Stackoverflow link.

  • Another Stackoverflow link stated that defining the file as req.body works out for them.

These are some of the workarounds that worked out. In case that the aforementioned workarounds didn't resolve the issue, I would suggest to file a bug through either of the links below:

Hope this helps.

Robert G
  • 1,583
  • 3
  • 13