Express Framework: Problems in Writing Login Pages

  node.js, question

There was a problem writing the login page. Code directly

The following is the foreground code.

<script>
 window.onload = function() {
 $("#login").click(function(){
 var username = $("#username").val();
 var password = $("#password").val();
 var data = {"username":username,"password":password};
 $.ajax({
 url:'login',
 type:'post',
 data: data,
 success: function(data,status){
 if(status == 'success'){
 location.href = 'manage_menu';
 bracket
 },
 error: function(data,status){
 if(status == 'error'){
 Alert ("Wrong username or password!  Please re-enter. "  )
 location.href = 'login';
 bracket
 bracket
 });
 })
 bracket
 </script>

The following is the code of node response. If the user name is fixed here like this, there is no problem. If the verification is successful, the status(200) will be returned and the jump will be successful.

router.post('/login',function(req,res){
        var user={
            username:'admin',
            password:'admin'
        }
       if(req.body.username==user.username&&req.body.password==user.password){
            req.session.user = user;
            res.sendStatus(200);
            }else{
                req.session.error = "用户名或密码不正确";
                res.sendStatus( 404 );
                    }
});

However, if I write like this, I won’t jump, and if there is success there.

router.post('/login',function(req,res){
  
      dao.User(req.body.username).then(function (result) {
      var user={
            username:result[0].username,
            password:result[0].password
        }
         if(req.body.username==user.username&&req.body.password==user.password){
          
            res.sendStatus(200);
            req.session.user = user;

            }
        else{
             res.sendStatus(404);
             req.session.error = "用户名或密码不正确";
              }
       });       
});

This is a database query

function User(username) {
 Varresult = db.msql.query ('select * from userwhere username = "'plus username plus'"');
 return result;
 bracket
 exports.User  = User;

Please help me to see why, thank you.
Add: The reason why you don’t jump is whether you returned to a status= “”status first or asynchronous. I don’t know how to change it. Please ask Great God for help, Kneeling xie. . .

You can try this:
After comparing the user name and password, use the

return res.json({});

Back, the front end should not judge the status of ajax and set a status to use by itself.