NodeJS, express validator working fine in routes, but error when importing from controllers

Viewed 9

To brief I am new to NodeJS and am trying to make a signup page with backend validation, the tool for the job I am using is express-validator. The validation works fine when I type it into the routes file, however I am trying to split the code into a controller file and I am getting issues, It is telling me that a comma should not be a certain point when I use it in the controller file but when I use it in the routes file it works fine, I know the code is correctly being imported as I also render the page with the same route/controller pair (I have omitted that part of the code for brevity)

SignupRoutes.js

const express = require('express');
const router = express.Router();

const signupController = require('../controllers/signupController');
router.post('/', signupController.serverSideEmailValidation);

module.exports = router;

signupController.js (Line 6 is the error, the comma at the end of the line)

const { check, validationResult } = require('express-validator');
const User = require('../models/userModel');
const serverSideEmailValidation = 
check('email')
    .notEmpty().withMessage('Email cannot be empty')
    .isEmail().withMessage('Email is invalid'),
    (req, res) => {

    const errors = validationResult(req);
    if(!errors.isEmpty()){
        return res.status(400).json({errors: errors.array()});

    };
    const user = new User({
        email: req.body.email,
        password: req.body.password
    }); 

    user.save();
    res.redirect('verify-email');
    };

module.exports = {serverSideEmailValidation}

Thanks for your time

0 Answers
Related