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

204
Visualizações
How can I print a property value in the error object?

I'm making a simple user register web app. I'm using MongoDB and Express JS to register new users and save them in the database.

I've put a few validation properties in the schema, whenever a user fills invalid data that doesn't follow the schema, a validation error will be displayed in the console.

Now, what I'm trying to do is that the validation error that is written in the console has two properties that I want, which are "path" and "kind", so I can display these two in the browser for the user to see.

How can I get just the "path" and the "kind" properties out of the error object? I don't want the whole error message printed, I just want these two properties.

I tried using console.log(err.path) and console.log(err.kind), but they both return undefined.

I couldn't figure out how to access those two properties.

// Post new user (REGISTER):
router.post('/signup', async (req, res) => {

    const username = req.body.username;
    const email = req.body.email;
    const password = req.body.password;

    try {
        await User.create({username, email, password})
    } 

    catch(err) {

// I don't want the whole err, I just want err.path and err.kind if possible!
        console.log(err);
    }

});

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The following should work. These properties are within a nested object, held by keyword errors

console.log(err.errors.username.kind, err.errors.username.path)
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