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

218
Vistas
Validation for existing email in table database(sql), node.js

I have a web formulary from where i extract data. The unique field should be the email, but if i repeat the email my formulary breaks and throws error, i don't know how to stop my formulary to break, i just want a error.push("reenter your email"), withouth connecting again to the db using npm start. I am looking for a validation or smth, i am beginner into node.js and working with js

connection.query(
    "SELECT * FROM `employees` WHERE `email`= ?",
    [employee.email],
    function (err, row) {
      if (err) throw err;
      else {
        if (row && row.length) {
          error.push("Email already exists!"); //this does nothing
          return console.log("Email is existing already!");
        }
      }
    }
  );
if(error.length === 0){
          // insert data in the table
}
else {
    res.status(400).send(error);
    console.log("Error");
  }

I want error.push("Email already exists!"); to modify my error array so it won't enter in the first if(error.length === 0)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

connection.query is an asynchronous function. So error.push happens after your if.

Possible solutions -

  1. Move the further code into the callback function
  2. Create a Promise and resolve it in the function, aka use the async/await system. Example function:
const executeQuery = (con, query, params = null) => {
    return new Promise((resolve, reject) => {
        con.query(query, params, (err, res) => {
            if (err) {
                reject(err);
                return
            }
            resolve(res);
        });
    })
};

You can run this, and you can say await executeQuery(con,query,param) and proceed from there.

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