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

208
Visualizações
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 Respostas
Responde à pergunta

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