Adrian Nadeau
Adrian Nadeau

Reputation: 21

Node Express Post 500 (Internal Server Error) jquery-3.4.1.min.js

I've been working on a node js/express js app for some time now. I have get's and post's that work from jquery $ajax calls.

For some reason I have one post "loginUser" that isn't getting into the user route post method

This was working previously but now has stopped, and as any programmer would say "has stoppped working for some reason".

I don't beleive anything was changed. I've tried using GET's, new POST's names, etc.

Every time it only provides a 500 error without any information in the error object.

Loginpage.ejs Form:

<form class="form-horizontal" id="loginUserForm" >

                    <div class="controls">

                      <strong>Email</strong><br/><input type="email" class="form-control custom-input-width" id="login-email" value="[email protected]" required>

                  </div>
                  <div class="controls">

                      <strong>Password</strong><br/><input type="password" class="form-control custom-input-width" minlength="6" id="login-password" value="Asialouie!123" required>

                  </div>

                    <div class="control-group">
                  <div class="controls text-center">
                      <br/>
                      <input type="submit" class="btn btn-green" id="button-signup" value="Sign in">

                      <br/><p class="aligncenter"><br/>
                  Don't have an account? <a href="/register" >Sign up</a>
                  <br/>Forgot password? <a href="/sendRestForm">Reset</a>
                </p>


                </div>
          </form>

JQuery Ajax code:

 $("#loginUserForm").submit(function(event) {
       // Prevent the form from submitting via the browser.
     event.preventDefault();
    try{
        const email = $("#login-email").val();
        const password = $("#login-password").val();
        console.log("email: "+email);
        console.log("pass: "+password);
        var data = {};
            data.email =email;
            data.message =password;

        $.ajax({
                url: '/users/loginUser',
                type: 'POST',
                data: JSON.stringify(data),
                contentType: 'application/json',
                /**
                * A function to be called if the request succeeds.
                */
                success: function(data, textStatus, jqXHR) {


                    window.location.assign("/profile/?id="+data._id);
                },
                error : function(error) {
                    //alert(error.name + ': ' + error.message);
                    console.log(error);
                    $(".alert-signin").show();
                }
        });
       }
       catch(err) {
        console.log("err: "+err.message);
      }


});

User Route Method:

router.post('/loginUser', function(req, res) {
  console.log("in user login");
});

This route Post works:

router.post('/',function(req,res){
    logger.info("email: "+req.body.email);
    User.findOne({ email:req.body.email}, function (error, user) {
        if(user){
            console.log("user exists");
            const error = new Error('User email account already exists.');
            res.status(410);
            res.send(JSON.stringify(error.message));

        }
        else{
          //save user  

          var pwd = req.body.password;

          bcrypt.genSalt(10, function(err, salt) {
              if (err) {
                logger.error("BCrype issue");
                const error = new Error("Unable to register, please try again.");
                //throw new Error('User email account already exists.');
                res.status(420);
                res.send(JSON.stringify(error.message));

              } else {
                  //console.log('Salt: ' + salt);
                  bcrypt.hash(pwd, salt, function (err, hash) {
                      if (err) {
                        logger.error("ERROR! users bcrypt");
                        const error = new Error("Unable to register, please try again.");

                        res.status(420);
                        res.send(JSON.stringify(error.message));
                      } else {

                        var user = new User({

                          firstname:req.body.firstname,
                          lastname :req.body.lastname,
                          email :req.body.email,
                          password : hash,
                          public:1,
                          admin: false,
                          bio : "",
                          location : "",
                          avatar: "",
                          url: "",
                          activated:"n"
                        });
                          user.save(function (error, user) {
                              if (error){ 

                                res.send(error.message);


                              }

                              res.send(user)
                          });    
                        }
                  });
              }
          });

    }

  });
 });   

I expect the route to take the login information and get into the LoginUser route and output "in user login" Here is the console output when the login form is posted:

POST http://localhost:3000/users/loginUser 500 (Internal Server Error) jquery-3.4.1.min.js:2

I don't understand why it's showing jquery-3.4.1.min.js:2 in the console for the 500 error.

Any help would be greatly appreciated.

Upvotes: 1

Views: 1254

Answers (1)

Vijay Kumar
Vijay Kumar

Reputation: 197

I think you should try the followings:

  1. You should put method attribute in the form tag
  2. If you have the following statement in your server code, remove it or comment it down.
app.use(bodyParser.json());
  1. If you have any attributes in the script tag in which jQuery is written, then remove them.
  2. What status code 500 is normally there is some error in the server-side code ( in the root ). Double-check the route code.

These all suggestions are what I did with my code when I faced the 500 error.

Upvotes: 0

Related Questions