这个快递代码有什么问题吗?

时间:2016-08-25 21:35:44

标签: mysql node.js express

你好我试图检查两个mysql值,如果它匹配数据库中的任何东西,它需要生成一个令牌,但它似乎不起作用:( 每次运行此代码我都会有一个连接超时:

var express = require('express');
var router = express.Router();
var mysql = require("mysql");
var randomstring = require("randomstring");
/* GET users listing. */
router.get('/', function(req, res, next) {
  // First you need to create a connection to the db
var connection = mysql.createConnection({
  host: "localhost",
  user: "segfault",
  password: "wnk9ctte2endcKzBKtre7auE",
  database: "segfault"
});
connection.connect();
var input_user = req.body.username;
var input_pass = req.body.password;
var token = randomstring.generate(12);
connection.query('SELECT username FROM segfault.users AS username', function(err, rows, fields) {
  if (err) throw err;

  for(var i in rows){
    username = rows[i].username;
    if(input_user == username){
      connection.query('SELECT password FROM segfault.users AS password', function(err, rows, fields) {
          if(rows[i].password == input_pass){
              res.send("OK: "+ token);
              console.log("OK:" + token)
          }
      });

    }
  }

});
connection.end();

});

module.exports = router;
请告诉我,我做错了什么!

1 个答案:

答案 0 :(得分:0)

关闭与数据库的连接,而不等待查询结果。

connection.end();之后的回调中移动res.send