Search code examples
node.jsmongodbexpressmongoosemongoose-schema

Mongoose validation error, "email is not defined"


I am new to mongoose and express. I try to create a simple login backend, however when send a post request with

{ "userEmail": "abc@xyz", "password": "pswrd" }

I get "email is not defined" error whose type is "VALIDATION". My User Schema is as follows:

const mongoose = require("mongoose");
const bcrypt = require("bcrypt");

const UserSchema = new mongoose.Schema({
  email: {
    type: String,
    required: [true, "Email is required"],
    trim: true,
    unique: true,
  },
  password: {
    type: String,
    trim: true,
    required: [true, "Password is required"],
  },
  username: {
    type: String,
    required: [true, "Username is required"],
    trim: true,
    unique: true,
  },
});

UserSchema.pre("save", async function (next) {
  const user = await User.findOne({ email: this.email });
  if (user) {
    next(new Error(`${this.email} already taken`));
    return;
  }

  const user1 = await User.findOne({ username: this.username });
  if (user1) {
    next(new Error(`${this.username} already taken`));
    return;
  }

  const salt = await bcrypt.genSalt(8);
  this.password = await bcrypt.hash(this.password, salt);
  next();
});

// userSchema.statics is accessible by model
UserSchema.statics.findByCredentials = async (email, password) => {
  const user = await User.findOne({ email });
  if (!user) {
      throw Error("User does not exist.");
  }
  const isMatch = await bcrypt.compare(password, user.password);
  if (!isMatch) {
      throw Error("Unable to login");
  }

  return user;
};

const User = mongoose.model("User", UserSchema);
module.exports = User;

I use findByCredentials to check if the User is in my mongoDB database or not. Finally, my login.js is as follows:

const express = require("express");
const mongoose = require("mongoose");
const User = require("../db/models/User");

const loginRouter = express.Router();

loginRouter.get("/api/login2", (req, res) => res.send("In Login"));

loginRouter.post("/api/login", async (req, res) => {
  const { userEmail, password} = req.body;

  if (!validateReqBody(userEmail, password)) {
    return res
      .status(401)
      .send({ status: false, type: "INVALID", error: "invalid request body" });
  }

  try {

    const newUser = new User({
        email: userEmail,
        password: password,
    });
    await newUser.findByCredentials(email, password);
} catch (error) {
    const validationErr = getErrors(error);
    console.log(validationErr);
    return res
      .status(401)
      .send({ status: false, type: "VALIDATION", error: validationErr });
  }

    res.send({ status: true });
});

//user.find --> mongoose documentation

// Validates request body
const validateReqBody = (...req) => {
  for (r of req) {
    if (!r || r.trim().length == 0) {
      return false;
    }
  }
  return true;
};


// Checks errors returning from DB
const getErrors = (error) => {
  if (error instanceof mongoose.Error.ValidationError) {
    let validationErr = "";
    for (field in error.errors) {
      validationErr += `${field} `;
    }
    return validationErr.substring(0, validationErr.length - 1);
  }
  return error.message;
};


module.exports = { loginRouter };

Thank you.


Solution

  • Definition of findByCredentials() is in User model. I was trying to reach that function by the object instance newUser that i created in login.js. However, i should have called the function as User.findByCredentials(email, password).