Reputation: 18127
exports.admin= function(req, res, next){
if(!req.user)
{
var err = new Error ('No Valid User');
err.status = 403;
return next(err);
}
else if(!req.user.admin)
{
var err = new Error ('You must be an administrator!');
err.status = 403;
return next(err);
}
else
{
console.log('IN'); // <---- I hit this
return next();
}
}
So I have this function and it is called here:
routerA.route('/').post( (req,res,next)=> {
authenticate.admin(req, res, next)
.then(()=>{ // <-----here on this line I receive this exception
console.log('AAA')
Dishes.create(req.body)
.then((dish)=>{
console.log('Dish Created: ', dish);
res.statusCode = 200;
res.setHeader('Content-Type','application/json');
res.json(dish);
}, (err)=>next(err))
.catch((err)=>next(err));
});
})
I don't get it I return the promise from admin, why I receive this exception Cannot read property 'then' of undefined.
Upvotes: 0
Views: 29
Reputation: 74879
next()
is not a promise in express. It's a callback to tell express to move onto the next middleware, whatever that may be.
Call authenticate.admin
as middleware. Then the call to next()
will allow express to move onto your request handler.
routerA.route('/').post(authenticate.admin, (req,res,next)=> {
...
})
Upvotes: 1
Reputation: 133168
But you are returning next()
which doesn't return a promise. It returns undefined
.
Upvotes: 0