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

184
Visualizações
Checking for existance of value in MySQL

I am developing a user authentication system with expressjs. Therefore to store user data i use a mysql database... When I query to my database I do it with this: SELECT password FROM USERS WHERE email=EmailHere
The problem with this is I dont know an elegant solution to returning an error message if the email doesnt exist. Somewhat understandable?

So to conclude: I want to know if that value exists when I query to the db so I dont get an error message.

Edit:

  const query = `SELECT password FROM USERS WHERE email=?;`;
  let [rows, fields] = await db.promise().query(query, [req.body.email]);

  const password = rows[0].password;
  console.log(password);

This gives me the error: Cannot read properties of undefined reading 'password'.

So it is trying to access the .password column. Can I prevent that?

Thanks in advance! Have a great day

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

0

Ok it turns out:

If I check if the length of the rows Array > 0 it fixes the problem. In the else statement I can just log out an error message to say the user does not exist. This could look something like this:

if (!rows.length > 0) return res.status(400).send('User doensnt exist!');
const password = rows[0].password;
// Declaring the variable after I know it exists.

Thanks to @danblack for this answer!

about 4 years ago · Juan Pablo Isaza Relatório

0

try to change the query like that

SELECT password FROM USERS WHERE email like '%';

or

SELECT password FROM USERS WHERE email='%';

please if you find it a solution don't forget to mark it as answer .

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