Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!