I can’t figure out why I am getting an error with this middleware function:
const { joiValidation } = require("../models/menu");
module.exports = function(req, res, next) {
const { error } = joiValidation(req.body);
if (error) return res.status(400).send(error.details[0].message);
next();
};
The console prints: “TypeError: next() is not a function”
What is going on?, I’m simply passing this middleware to my routes, here’s 2 examples:
const validate = require("../middleware/validate");
router.post("/", validate, async (req, res) => {
const item = new Menu({
title: req.body.title,
description: req.body.description,
price: req.body.price,
category: req.body.category
});
await item.save();
res.send(item);
});
router.put("/:id", [validate, validateObjectId], async (req, res) => {
const item = await Menu.findByIdAndUpdate(
req.params.id,
{
title: req.body.title,
description: req.body.description,
price: req.body.price,
category: req.body.category
},
{ new: true }
);
if (!item) {
return res.status(404).send("Resource with the given ID was not found.");
}
res.send(item);
});
Why is it not working?