1

Inside my routes/index.js file I have:

const express = require("express");

const authController = require("../controllers/authController");


const router = express.Router();

console.log('ROUTES index.js')


/* Error handler for async / await functions */
const catchErrors = fn => {
    return function(req, res, next) {
        return fn(req, res, next).catch(next);
    };
};

/**
 *  AUTH ROUTES: /api/auth
 */
router.post(
    "/api/auth/signup",
    authController.validateSignup,
    catchErrors(authController.signup)
);

module.exports = router;

and then inside the authController.js I have:

const { body, check, sanitizeBody, validationResult } = require('express-validator');




exports.validateSignup = async (req, res, next) => {
    console.log('validateSignup')

    await check("name").notEmpty().withMessage("Enter a Name")
                .isLength({ min: 4, max: 10 }).withMessage("Name must be between 4 and 10 characters").run(req);


    const result = validationResult(req);


    if (!result.isEmpty()) {
      console.log('inside if of validateSignup')
      return res.status(400).json({ errors: result.array() });
    }

    console.log('before next')
    next();
  };
  

I then use axios to post to that specified route:

const { data } = await axios.post("/api/auth/signup", user);

With the above line if I enter the correct inputs I don't get any errors and therefore the console.log inside the if statement is not called... but I get the following output and errors:

validateSignup
before next
TypeError: fn is not a function
    at /Users/bliss/Documents/Coder/NextJS/NextManilaBNB/server/routes/index.js:14:16
    at Layer.handle [as handle_request] (/Users/bliss/Documents/Coder/NextJS/NextManilaBNB/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/bliss/Documents/Coder/NextJS/NextManilaBNB/node_modules/express/lib/router/route.js:137:13)
    at exports.validateSignup (/Users/bliss/Documents/Coder/NextJS/NextManilaBNB/server/controllers/authController.js:23:5)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

As you can see the error seems to happen after calling next()

How to solve this? What is causing this?

preston
  • 3,721
  • 6
  • 46
  • 78

0 Answers0