Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

114
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda