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();
});
});
);