express route mysql,Node.js Express /路由和mysql

I'm facing a little problem. I'm trying to make a small API to check if a user already exists in my DB.

I use express and routes so i can query like "http://example.com/check/user/john" and it should output : true or false depending if the user exists or not in the DB.

In my route file i have the code below :

var mysql = require('mysql');

var pool = mysql.createPool({

host: 'localhost',

user: 'root',

password: '123456',

database: 'nodejs'

});

module.exports = function(app){

app.get('/register/check/u/:username', function(req, res){

var output = 'false';

pool.getConnection(function(err, conn) {

query = conn.query('SELECT * FROM users WHERE username LIKE ?', [req.params.username]);

query.on('error', function(err){

throw err;

});

query.on('result', function(row){

var output = 'true';

console.log(row.email);

console.log(output);

});

conn.release();

});

res.render('register/check_username', { output: output});

});

);

The problem is that res.render('register/check_username', { output: output}); will always output : false, but in my console log it shows true when the user already exists.

Any idea of why the output variable isn't updated ?

Solution :

As explained by akaphenom, the pool.getConnection was beeing initialized into a non-blocking process and therefore my first code was automaticaly sending output as false. Here's the working code :

app.get('/register/check/u/:username', function(req, res){

pool.getConnection(function(err, conn) {

var output = 'false';

query = conn.query('SELECT * FROM users WHERE username LIKE ?', [req.params.username]);

query.on('error', function(err){

throw err;

});

query.on('result', function(row){

output = 'true';

});

query.on('end', function(result){

res.render('register/check_username', { output: output});

});

conn.release();

});

});

解决方案

I believe you not allowing for the non-blocking nature of these calls. The variable is set to false the connection is called and then falls through pending a call back. You immediately render the response, prior to the call back completing.

module.exports = function(app){

app.get('/register/check/u/:username', function(req, res){

// you set the value of the output var

var output = 'false';

// this is a non-blocking call to getConnection which fires the callback you pass into it, once the connection happens. The code continues on - it doesn't wait.

pool.getConnection(function(err, conn) {

query = conn.query('SELECT * FROM users WHERE username LIKE ?', [req.params.username]);

query.on('error', function(err){

throw err;

});

query.on('result', function(row){

var output = 'true';

console.log(row.email);

console.log(output);

});

conn.release();

});

// you are getting here before the callback is called

res.render('register/check_username', { output: output});

});

);

Why do you get the right value in the console? Because eventually the callback is called and performs what you expect. It is just called after the res.render

THis is more likely the code you want:

module.exports = function(app){

app.get('/register/check/u/:username', function(req, res){

pool.getConnection(function(err, conn) {

query = conn.query('SELECT * FROM users WHERE username LIKE ?', [req.params.username]);

query.on('error', function(err){

throw err;

});

query.on('result', function(row){

var output = 'true';

console.log(row.email);

console.log(output);

res.render('register/check_username', { output: output});

});

conn.release();

});

});

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值