I started using Firebase Functions and ran into a problem. Namely, when I created my API (running locally), deployed to Firebase Functions using firebase deploy --only functions
and called from my frontend – in the function logs an error is displayed:
Error: Unexpected end of form
at Multipart._final (/workspace/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)
My goal is to receive the file in the API and process it using the easy-template-x
bibiotech. When I used multer
for this, the problem was the same
index.js
const functions = require('firebase-functions');
const routes = require('./src/routes/routes');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
app.use('/', routes);
app.listen(3009, () => {
console.log('Listening on 3001...');
});
exports.app = functions.https.onRequest(app);
routes.js
const checkErrors = require('../controllers/checkErrors');
const express = require('express');
const router = express.Router();
router.post('/checkErrors', checkErrors);
module.exports = router;
And checkErrors.js
const {TemplateHandler} = require('easy-template-x');
const handler = new TemplateHandler();
const busboy = require('busboy');
const checkErrors = async (req, res) => {
const bb = busboy({headers: req.headers});
let templateBuffer = null;
let jsonData = '';
bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
file.on('data', (data) => {
templateBuffer = data;
});
});
bb.on('field', (fieldname, val) => {
jsonData += val;
});
bb.on('finish', async () => {
jsonData = JSON.parse(jsonData);
console.log(jsonData);
const templateTags = await handler.parseTags(templateBuffer);
const templateTagsNames = templateTags.map(({name}) => name);
//Znajdź brakujące wartości w Excel i je zidentyfikuj
const missingValues = [];
jsonData
.filter((row) => {
return !templateTagsNames.every((name) => Object.keys(row).includes(name));
})
.forEach((row) => {
templateTagsNames.forEach((templateName) => {
if (!row.hasOwnProperty(templateName)) {
missingValues.push({fieldName: templateName, rowNum: row.rowNum + 1});
}
});
});
res.json({
errors: missingValues,
});
});
return req.pipe(bb);
};
module.exports = checkErrors;