title: Node JS - UnhandledPromiseRejectionWarning
body: <p>I'm using Node and MongoDB to create a team grouping and have run into a node console error:</p>
  
  <blockquote>
    <p>UnhandledPromiseRejectionWarning: Unhandled promise rejection
    (rejection id: 2): TypeError: Cannot read property 'apply' of
    undefined</p>
  </blockquote>
  
  <p>I'm using Axios to post a request to an my Node JS API that should create a new entry in my MongoDB. Here is my code:</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>const mongoose = require('mongoose');
  const Team = mongoose.model('Team');
  const promisify = require('es6-promisify');
  
  
  exports.validateTeamName = (req,res,next) =&gt; { 
  
      req.sanitizeBody('teamName');
      req.checkBody('teamName','You must supply a name!').notEmpty();  
      req.sanitizeBody('userId');
      req.checkBody('userId','There was an error with your user id - please log out and log back in!').notEmpty();  
      const errors = req.validationErrors();
      if(errors) {
          req.flash('error',errors.map(err =&gt; err.msg));
          res.json(req.flash())
          return; //if theres errors stop the function from running
      }
      next();
  };
  
  exports.register = async (req,res,next) =&gt; {
  
      const team = new Team({
                  teamName:req.body.teamName,
                  owner:req.body.userId
      })
  
      const register = promisify(Team.register,Team);
      await register(team,req.body.password);
  
          next()
  
  }
  
  exports.finishCreateTeam = async (req,res,next) =&gt; {
      console.log('all done')
  }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  mongoose.Promise = global.Promise;
  const md5 = require('md5');
  const validator = require('validator');
  const mongodbErrorHandler = require('mongoose-mongodb-errors');
  const passportLocalMongoose = require('passport-local-mongoose');
  
  const teamSchema = new Schema({
      teamName:{
          type:String,    
          trim:true,     
          required:'Please supply a team name'        
      },
          owner:{
          type:String,    
          trim:false,   
                  required:'Please supply a user ID'    
          }
  });
  
  teamSchema.plugin(mongodbErrorHandler);
  
  module.exports = mongoose.model('Team',teamSchema);
  </code></pre>
  
  <p><strong>Route:</strong></p>
  
  <pre><code>router.post('/createteam',
      manageTeamController.validateTeamName,
      manageTeamController.register,
          manageTeamController.finishCreateTeam
  );
  </code></pre>
  
  <p>I used very similar functionality to create users so i'm not sure where i've gone wrong here, plus i'm not entirely sure what the error means.. When I comment out the await <code>register(team,req.body.password);</code> the error goes away so I guess this is where i've gone wrong but not sure how!</p>
  
  <p>Here is the stack trace i'm getting after adding in the unhandledRejection log that was suggested:</p>
  
  <pre><code>[] Promise {
  []   &lt;rejected&gt; TypeError: Cannot read property 'apply' of undefined
  []     at C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\es6-promisify\dist\promisify.js:75:40
  []     at Promise (&lt;anonymous&gt;)
  []     at C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\es6-promisify\dist\promisify.js:54:20
  []     at exports.register (C:\Users\BenLi\Desktop\development projects\note app\new\server\controllers\manageTeamController.js:29:11)
  []     at Layer.handle [as handle_request] (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\layer.js:95:5)
  []     at next (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\route.js:137:13)
  []     at exports.validateTeamName (C:\Users\BenLi\Desktop\development projects\note app\new\server\controllers\manageTeamController.js:18:5)
  []     at Layer.handle [as handle_request] (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\layer.js:95:5)
  []     at next (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\route.js:137:13)
  []     at Route.dispatch (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\route.js:112:3)
  []     at Layer.handle [as handle_request] (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\layer.js:95:5)
  []     at C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:281:22
  []     at Function.process_params (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:335:12)
  []     at next (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:275:10)
  []     at Function.handle (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:174:3)
  []     at router (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:47:12)
  []     at Layer.handle [as handle_request] (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\layer.js:95:5)
  []     at trim_prefix (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:317:13)
  []     at C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:284:7
  []     at Function.process_params (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:335:12)
  []     at next (C:\Users\BenLi\Desktop\development projects\note app\new\server\node_modules\express\lib\router\index.js:275:10)
  []     at app.use (C:\Users\BenLi\Desktop\development projects\note app\new\server\app.js:73:3) }
  </code></pre>
  
  <p>Here is line 73 in app.js:</p>
  
  <pre><code>app.use((req, res, next) =&gt; {
    req.login = promisify(req.login, req);
    next();
  });
  </code></pre>