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

202
Vistas
Alert function in Node.js not working in some cases

I'm a newbie at node.js, and trying to make a login page.

When I'm trying to login with the right credentials the page works as intended. Also worth noting is that when I try to login with no credentials the alert window pops up. But when I try to login with the wrong credentials, it just won't work. I've tracked the error back to "alert" function, but I cannot understand why in the world the alert function works when calling it to display the alert that no username nor password was inserted, but will crash when I'm trying to display that the username and password are wrong. I'm pretty certain that the error comes from the alert function.

The code is below.

Thanks alot!

app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
    connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
        if (results.length > 0) {
            request.session.loggedin = true;
            request.session.username = username;
            response.redirect('/home');
            response.end();
        } else {
            alert('Incorrect Username and/or Password!');
        }
    });
} else {
    alert('Please enter Username and Password!');
}
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

but I cannot understand why in the world the alert function works when calling it to display the alert that no username nor password was inserted

It shouldn't do.

alert isn't a function provided by Node.js. It belongs to browsers so it can show something in the browser UI.

To send something from a server built on Node.js back to the browser then:

  • The something needs to be data
  • You need to send it by calling a method (like render, json or send) on the response object.
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