Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
User.findUserByEmail is not a function

I'm using nodejs and mongoose , using express-validator to validating email duplicate,

  check("email")
    .custom(value => {
        return User.findUserByEmail(value).then(user => {
        if (user) {
            return Promise.reject('E-mail already in use');
        }
        });
    })

and when posting with duplicated email i've got error saying

{
  "errors": [
    {
      "value": "boka@gmail.com",
      "msg": "User.findUserByEmail is not a function",
      "param": "email",
      "location": "body"
    }
  ]
}

I've used reference as https://express-validator.github.io/docs/custom-validators-sanitizers.html#example-checking-if-e-mail-is-in-use

I tried to use other method such as findOne

.custom(value => {
        return User.findOne({value}).then(user => {
        if (user) {
            return Promise.reject('E-mail already in use');
        }
        });
    })

It does works but , It return response as duplicate even if email is not duplicate but writes to db

{
  "errors": [
    {
      "value": "boka1@gmail.com",
      "msg": "E-mail already in use",
      "param": "email",
      "location": "body"
    }
  ]
}

Is there something i'm missing thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In mongoose, there is no such method on models called findUserByEmail that's why you are getting that error.

You can use findOne instead. If you insist, I think you should define it yourself in your model.

User.findUserByEmail = (email) => {
  return this.findOne({ email: email })
  // or return User.findOne({ email: email })
}
about 4 years ago · Juan Pablo Isaza Report

0

.custom((value, { req }) => {
    return new Promise((resolve, reject) => {
        User.findOne({ email: req.body.email }, function(err, user) {
            if (err) {
                reject(new Error("Server Error"));
            }
            if (Boolean(user)) {
                reject(new Error("E-mail already in use"));
            }
            resolve(true);
        });
    });
}),

check this ...

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!