I am having an error when the user is not admin, it doesn't display my message "Not authorized to access this page" because instead I'm getting this error in the console:
TypeError: productController.createProduct(...).then is not a function
I have a router set up like this:
router.post('/create',auth.verify, (req, res)=>{
const isAdmin = auth.decode(req.headers.authorization).isAdmin;
productController.createProduct(req.body,isAdmin).then(result => res.send(result));})
And my controller:
module.exports.createProduct = (reqBody, isAdmin) => {
if (isAdmin === true) {
let newProduct = new Product({
name: reqBody.name,
description: reqBody.description,
price: reqBody.price,
});
return newProduct.save().then((result, error) => {
return error ? false : 'Successfully created a product';
});
} else {
return 'Not authorized to access this page';
}
};
Everything works fine though when the user is an admin. I wonder if this has something to do with how I return my message when the user is not admin. Because I am using promises?