Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda