What is wrong with my middleware function?

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?

If I comment out next() from the middleware function then I get a different error: “Error: Route.put() requires a callback function but got a [object Undefined]”

module.exports = function(req, res, next) {
  const { error } = joiValidation(req.body);
   if (error) return res.status(400).send(error.details[0].message);

  //next();
};

It’s my understanding the request should end up handling if we don’t call next in our middleware functions, but here I am getting yet another error and I can’t find the bug :confused:

NVM, I found the issue, basically when I was doing refactoring I had a reference to the middleware function in other routes and I didn’t refactored the code in those routes.