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

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

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 Denunciar

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 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