1

I am trying to validate user using below code:

function validateUser(adminEmailId, adminPassword) { try{ console.log('email id: '+adminEmailId+' password: '+ adminPassword); var connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'somePassword', database: 'someDatabase' }); var query = connection.query('select id from AdminData where adminEmail = "'+adminEmailId+'" AND adminPassword = "'+adminPassword+'"'); console.log('query: ' + query); query.on('error', function(error){ console.log('A db error occurred: '+error); }); query.on('result', function(result){ console.log('some result: '+ result); if(result.id === null) { console.log('user not found'); } else { console.log('user found :)'); } }); } catch (ex) { console.log('some exception ' + ex); } } 

The last two logs which it is printing in console are -

email id: [email protected] password: gfdgdf query: [object Object] 

It should have printed -

user not found // OR user found :) 

I tried verifying if SQL is running properly or not by firing this in command line:

mysql -h127.0.0.1 -uroot -p 

It showed me error:

Command not found 

So I updated the path by using below command:

export PATH="/usr/local/mysql/bin:$PATH" 

Then I was able to use mysql from command line, but still there is no success achieved in Node.js code :(

Any suggestions?

6
  • Did you try using the same query with the same values from the command line and see if it returns any rows? Commented Sep 9, 2014 at 3:12
  • Did you try returning the result as object? (In PDO you need to set that explicitly!). Commented Sep 9, 2014 at 3:18
  • @mscdex yeah I tried that and it returned: Empty set (0.00 sec), in that case it should have printed in console: 'user not found', which it didn't :( Commented Sep 11, 2014 at 2:58
  • @DOCASAREL please elaborate on: "returning the result as object? (In PDO you need to set that explicitly!)" Commented Sep 11, 2014 at 2:59
  • Did @mscdex solve the issue? Here the PDO one: php.net/manual/en/pdostatement.fetchobject.php. Here for ( deprecated ) MySQL: php.net/manual/en/function.mysql-fetch-object.php. All php, sorries. Thought would help! Commented Sep 11, 2014 at 9:52

1 Answer 1

5

If your query results in no error or result events, then that means the query resulted in an empty set. You should listen for the end event so that you know when the query is done executing. Then you can set some condition inside the result event handler that you check on end to see if the user was found or not.

Probably an easier way to do this is to just pass a callback to query() since it doesn't seem like you will need to be streaming rows in this case. Something like:

connection.query('select id from AdminData where adminEmail = "'+adminEmailId+'" AND adminPassword = "'+adminPassword+'"', function(err, rows) { if (err) { console.log('A db error occurred: ' + err); return; } if (rows && rows.length) console.log('user found :)'); else console.log('user not found'); }); 

Also I should point out a couple of other things:

  • You should not concatenate user-submitted data into SQL query strings as it leaves you susceptible to SQL injection attacks. You should at least escape the values, but it's recommended to use real prepared statements. The mysql module currently does not support real prepared statements, but mysql2 does. mysql2 is API-compatible with mysql and is substantially faster overall.
  • You should probably have a callback parameter at the end of your validateUser() parameter list so that you can call a callback when the query is finished, allowing you to appropriately continue execution of your code. Otherwise the place where validateUser() is called will have no idea when the query finishes and what the result was.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for useful suggestions, I will definitely try these :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.