0

I'm trying to make Register using EJS. Therefore, I'm checking

  1. If all input fields have values or not
  2. Is Email valid or not
  3. Password Length has to be < 6 characters
  4. Is Email already register or not And give them a message if they do not comply with the above conditions. To Check all these conditions I have the following code inside the userCtrl.js file userCtrl.js
const Users = require("../models/userModel");

const userCtrl = {
  //! Register User
  register: async (req, res) => {
    try {
      const { name, email, password } = req.body;
      // Check If All fields are filled with values or not
      if (!name || !email || !password) {
        return res.status(400).json({ masg: "Please fill allfields." });
      }
      // Check If email is valid
      if (!validateEmail(email)) {
        return res.status(400).json({ masg: "Please enter valid email." });
      }
      //Check password length
      if (password.length < 6) {
        return res
          .status(400)
          .json({ masg: "Password must be atleast 6 characters long." });
      }
      const user = await Users.findOne({ email });
      // Check If email is already registered
      if (await Users.findOne({ email })) {
        return res.status(400).json({ masg: "User already exists." });
      }
      res.json({ msg: "User Registered Successfully!" });
    } catch (err) {
      console.log(err);
      return res.status(500).json({ msg: err.message });
    }
  },
};

//! Exporting User Controller
module.exports = userCtrl;

Here is user Module for refrance.

const mongoose = require("mongoose");
const { Schema } = mongoose;

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, "Please enter your name"],
    trim: true,
  },
  email: {
    type: String,
    required: [true, "Please enter your email"],
    trim: true,
    unique: true,
  },
  password: {
    type: String,
    required: [true, "Please enter your password"],
    trim: true,
  },
  role: {
    type: Number,
    default: 0, // 0 for user, 1 for admin
  },
  avator: {
    type: String,
    default:
      "https://res.cloudinary.com/manix/image/upload/v1639722719/avator/istockphoto-1214428300-170667a_c4fsdt.jpg",
  },
});
//! Exporting User Modules
module.exports = mongoose.model("Users", userSchema);

But when I try to register users using Postman then I got this error. enter image description here

Please Help me to fix this issue.

  • You have not added the function definition for the validateEmail, due to which it is undefined in the call stack, and you are getting that error. – Diksha Goyal Dec 18 '21 at 08:15
  • https://stackoverflow.com/a/46181/10281244 you can refer this function to validate your email. – Diksha Goyal Dec 18 '21 at 08:17
  • You can also use this repo to do this in an easier and cleaner way: https://github.com/validatorjs/validator.js/ – jewishmoses Dec 18 '21 at 08:30

1 Answers1

0

The issue is probably happening here

if (!validateEmail(email))

Where is your validateEmail() function located?

gbac
  • 235
  • 3
  • 12