2

Hi I am trying to use a post method with busboy for multipart form-data and even though the code is working perfectly fine on testing it on localhost but when running on firebase function I am getting the below error :

In postman I am getting the response as : Error: could not handle the request

Let me know if you require anymore information from my end.

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)

If someone can please help me resolve this issue

Below is the code :

index.js

var busboy = require('connect-busboy');

app.use(busboy());


app.use(cors()); 

const portCheck = process.env.PORT || 3001

app.use(express.json({limit: "50mb"}));
app.use(express.urlencoded({limit: "50mb", extended: true}));

app.use(cookieParser());
// app.use(express.static(path.join(__dirname, '../public-flutter')));
// app.get('/', (_, res) => {
//   res.sendFile(path.resolve(__dirname, '../public-flutter/index.html'));
// });

userProfile.js

router.post('/userprofile/check/busboy', async (req,res) => {
  console.log(req.body);
  
  const fields = {};
  req.busboy.on('field', (name, val) => {
    console.log('reached till here');
    
      console.log(`Processed field ${name}: ${val}.`);
    
    
    //fields.set(name,val)
    fields[name] = val;
    console.log(fields);
  });

  req.busboy.on('finish', function() {
    console.log('Done parsing form!');
    res.status(201).send();
  });
  
  req.pipe(req.busboy);
})
akm
  • 149
  • 1
  • 9

1 Answers1

0
req.busboy.on('field', (name, val) => {
  fields[name] = val;
});

req.busboy.on('finish', () => {
  try {
    notify = new userCampaign(fields);
    notify.save((err,post) => {
      if (err) {
        console.log(err);
      } 
                     
      datacombine = new userCampaign({
        title: post.title,
        status: post.status,
      })
      datacombine.save((err,postcombine) => {
        if (err) {
          console.log(err);
        }
        res.status(201).send(postcombine);
      })  
    });
  } catch(e) {
  
  }
})
            
req.busboy.end(req.rawBody);
req.pipe(req.busboy)
})
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
akm
  • 149
  • 1
  • 9