Skip to content Skip to sidebar Skip to footer

How Can I Parse A Javascript Object Returned By Multer?

I am trying to use multer to handle upload of a csv file in express, then parse the file line by line. I am able to get the file as an object contained in req.body, but I am not ab

Solution 1:

In the example provided by the multer documentation, the req.body object will contain the text fields from the form, and req.file will contain the file that was uploaded:

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file// req.body will hold the text fields, if there were any
})

Have you tried using the file object? Something like the following:

router.post('/distributor/:id/upload', upload.single(), function (req, res, next) {
  req.setTimeout(600000);
  console.log(req.body)
  console.log(req.file)
  csv()
  .fromFile(req.file.path)
  .then((jsonObj)=>{
    console.log(jsonObj);
    //loop through array of objects here
  }) 
  next()
});

Post a Comment for "How Can I Parse A Javascript Object Returned By Multer?"