简体   繁体   中英

expressJs router.post() not working for multiple middleware giving [object Undefined] error

i am creating a REST API for my users, and i used express-validator for the validation before creating the user in the database.. but when i chain my middleware together in the router.py file it gives the error Error: Route.post() requires a callback function but got a [object Undefined] i imported my middleware and controller from thier respective files.

here is the code in my router.py

const express = require('express');
const authMiddleware = require('../middlewares/authMiddleware');
const authController = require('../controllers/authController');

const router = express.Router();

router.post(
  '/signup',
  authMiddleware.verifySignUpInit(),
  authMiddleware.verifySignUp,
  authController.signup
);

module.exports = router;

in my middleware file i added this..

const jwt = require('jsonwebtoken');
const { authentication } = require('../config');
const { User } = require('../models');
const { body } = require('express-validator');

const verifySignUpInit = () => {
  return [
    body('email', 'email is required').exists().normalizeEmail().isEmail(),
    body('phone', 'phone is required').exists().isInt(),
    body('first_name', 'first_name is required').exists().isString(),
    body('last_name', 'last_name is required').exists().isString(),
    body('password', 'password is required').exists(),
  ];
};

const verifySignUp = (req, res, next) => {
  const { email, phone, password } = req.body;

  User.findOne({
    email,
  }).exec((err, user) => {
    if (err) {
      res.status(500).json({
        status: 'error',
        message: err,
      });
    }
    if (user) {
      return res.status(400).json({
        status: 'failed',
        message: 'Email Provided Already Exists',
      });
    }
  });
  User.findOne({
    phone,
  }).exec((err, user) => {
    if (err) {
      res.status(500).json({
        status: 'error',
        message: err,
      });
    }
    if (user) {
      return res.status(400).json({
        status: 'failed',
        message: 'Phone Provided Already Exists',
      });
    }
  });
  const password_is_valid = authentication.passwordSchema.validate(password);
  if (!password_is_valid) {
    return res.status(400).json({
      status: 'failed',
      message: 'password requirements not met',
    });
  }
  next();
};

module.exports = {
  verifySignUpInit,
  verifySignUp,
};

and finally in my controller i have this..

const config = require('../config');
const db = require('../models');
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs');
const { validationResult } = require('express-validator');

const { User, Role } = db;
const { token_expiry_time, refresh_token_expiry_time } = config.authentication;

const signUp = (req, res) => {
  const { email, phone, first_name, last_name, password } = req.body;
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({
      status: 'failed',
      message: errors.array(),
    });
  }
  bcrypt.hash(password, 8, (err, hash) => {
    if (err) {
      return res.status(500).json({
        status: 'error',
        message: err,
      });
    }
    const user = new User({
      first_name: first_name,
      last_name: last_name,
      email: email,
      phone: phone,
      password: hash,
    });

    Role.findOne({ name: 'is_user' }, (err, role) => {
      if (err) {
        res.status(500).json({
          status: 'error',
          message: err,
        });
        return;
      }
      user.roles = [role._id];
      user.save((err) => {
        if (err) {
          return res.status(500).json({
            status: 'error',
            message: err,
          });
        }

        return res.status(201).json({
          status: 'success',
          data: {
            user,
          },
        });
      });
    });
  });
};


module.exports = {
  signUp,
};

i cant tell what i am doing wrong :(

authController.signup 

should be:

authController.signUp

You have to use the same capitalization for the property name you exported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM