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

153
Visualizações
How to use the doValidate method of mongoose?

I have the following schema

const mySchema = new mongoose.Schema({
   kind: {
      type: String,
      enum: { 
         values: ["dollar", "euro", "pound", "lira"],
         message: "only dollar, euro, pound or lira are allowed"
      }
      required: [true, "kind field is required"]
   }
   // ... other fields
})

I have the following code:

app.post("/route", async (req, res, next) => {
    // 🏁 write code here
})

I want to validate only one path from that schema, I don't to validate everything, I just want to validate one field, so what I did according to this git discussion is the following:

app.post("/route", async (req, res, next) => {
    // 🏁 
    await mySchema.path("kind").doValidate(req.body.kind)
})

but this did not work and I got the following error:

.../node_modules/mongoose/lib/schematype.js:1332
        fn(err);
        ^

TypeError: fn is not a function
    at .../node_modules/mongoose/lib/schematype.js:1332:9
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

How can I use doValidate and get the benefit of it? and why there's no documentation on how to use it?

  • and is there a way I can promisify it?
about 4 years ago · Santiago Gelvez
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