Search code examples
node.jsexpresspromisees6-promisemulter

What is causing my request to timeout?


I am trying to create a route that allows a user to upload a csv file, then asynchronously parses the file as Json and instantiates a model for each line. I have tried to use promises to do so, but the function request keeps timing out and I cannot see where it is breaking. Here is the code(/app/index):

const csv=require('csvtojson');
const multer  = require('multer');
const upload = multer().single(); 

router.post('/distributor/:id/upload', (req,res) => { 
  return new Promise((resolve, reject) => {
    upload(req,res,function(err){
      if(err !== null) return reject(err);
      resolve();
    });
  })
  .then((req, res) => {
    return csv()
    .fromString(req.body.toString('utf8'))
    .on('json', (item) => { 
      item.distributor_id = req.params.id 
      Product
      .forge(item.body)
      .save()
      .then((product) => {
        res.json({id: product.id});
      })
      .catch((error) => {
        console.error(error);
        return res.sendStatus(500);
      })
    })
    .on('done', () => { 
      console.log('done parsing'); 
      resolve();
    });
  }) 
})

Here is the output from the heroku logs when I post a file to this route:

(node:49) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 2): undefined
2018-08-09T03:57:17.240599+00:00 app[web.1]: (node:49) [DEP0018] 
DeprecationWarning: Unhandled promise rejections are deprecated. In the 
future, promise rejections that are not handled will terminate the Node.js 
process with a non-zero exit code.
2018-08-09T03:57:47.205596+00:00 heroku[router]: at=error code=H12 
desc="Request timeout" method=POST path="/api/distributor/1/upload" host=fba- 
prof-prods.herokuapp.com request_id=40e1864b-fa71-49aa-8fdf-cedb1752edef 
fwd="73.92.68.83" dyno=web.1 connect=1ms service=30284ms status=503 bytes=0 
protocol=https

If you could point me to any resources/examples where something similar is done correctly (handling upload and asynchronously parsing a large csv file), I would also greatly appreciate that. I am at a loss as to how to do this and can't seem to find any good resources! Thanks.


Solution

  • You need to do something like this:

    function uploadAsync(req,res){
        return new Promise((resolve, reject) => {
             upload(req,res,function(err){
                 if(err !== null) return reject(err);
                 resolve();
             });
        });
    }
    

    Note the resolve(). This is the key here since you are not doing anything with that Promise.