Search code examples
expressasync-awaitpassport.jspassport-local-mongoose

passport-local-mongoose: UnhandledPromiseRejectionWarning: Unhandled promise rejection


I have a working pasport-local-mongoose implementation but am trying to move to async/await, and am getting an error.

Here's my controller:

module.exports.register = async (req, res, next) => {

    const user = new User(),
        fillable = [
            'title',
            'firstName',
            'lastName',
            'practice',
            'addressLine1',
            'addressLine2',
            'city',
            'state',
            'postcode',
            'jobTitle',
            'country',
            'locale',
            'userRole',
            'termsAccepted',
            'consentOptIn',
            'username',
            'password',
            'legacyUserId',
            'authType'
        ]

    populateModel(user, fillable, req.body) // this is just a helper to set the schema from the request

    const result = await User.register(user, req.body.password).catch(next)

    return responseHelper.handleSuccess(res, {
        user: result
    })

}

I have two response handlers in a helper module:

module.exports.handleSuccess = (res, data) => {

  return res.json({
    message: 'Success.',
    data: data
  })

}

module.exports.handleUnexpectedError = (res, err) => {

  return res.status(err.status || 500).json({
    message: err.message || 'Internal Server Error.',
    error: err
  })

}

My app has an error handler as middleware:

app.use((err, req, res, next) => responseHelper.handleUnexpectedError(res, err))

And finally my router that calls the controller method:

router.post('/auth/register', authController.register)

The success response is fine, and when an error is thrown and passed to the middleware on this line, it does return as expected:

const result = await User.register(user, req.body.password).catch(next)

However when that error is thrown, I get an error in the console:

POST /v1/auth/register 500 166 - 9.253 ms
(node:21488) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:518:11)
    at ServerResponse.header (/Users/russback/Sites/api.alignpatientexperience.co.uk/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/Users/russback/Sites/api.alignpatientexperience.co.uk/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/russback/Sites/api.alignpatientexperience.co.uk/node_modules/express/lib/response.js:267:15)
    at Object.module.exports.handleSuccess (/Users/russback/Sites/api.alignpatientexperience.co.uk/helpers/responseHelper.js:95:14)
    at module.exports.register (/Users/russback/Sites/api.alignpatientexperience.co.uk/controllers/authController.js:64:27)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:21488) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

It seems my success handler is being called as well as my error handler, but I'm not sure where I've gone wrong.


Solution

  • Consider wrapping the request in a try/catch block and call next callback within the catch block.

    module.exports.register = async (req, res, next) => {
    
       const user = new User(),
           fillable = [
                  ...
           ]
    
       populateModel(user, fillable, req.body) // this is just a helper to set the schema from the request
    
       try {
         const result = await User.register(user, req.body.password)
    
         return responseHelper.handleSuccess(res, {
           user: result
         })
    
       } catch(error) {
         next(error)
       }
    
    }