Callbacks are asynchronous! They are called after your function returns.
The sequence is:
enter login function
con.execQuery initiates query
return from login function (result variable is still empty array)
later:
node event loop invokes the callback
rows is assigned to result variable (too late, login returned before)
On Tuesday, March 28, 2017 at 2:28:47 AM UTC+2, SURAJ KUMAR CHANDRA wrote:
>
> How do you guys use node js vars. Please refer to red marking. I am
> declaring variable at one place trying to use at another place, simply not
> working.
>
> function login(email,password){ // Returns the login data row
> var email = email.trim().toLowerCase();
> var password = password.trim();
>
> var result = []; //*Declaring here*
>
> //var queryString = "SELECT * FROM users where Email ="+"'"+email+"'"+
> AND Password = +"'"+password+"'";
> var queryString = "SELECT * FROM users where Email ="+"'"+email+"' AND
> Password = '"+password+"'";
>
> //var execQuery = function execQuery(){
> con.query(queryString,function(err,rows){
> if(err) throw err;
>
> // console.log('Data received from Db:\n');
> //result = result.push('SUCCESS','0000','Data',rows)
> //console.log(rows);
> result = rows; // *Want to use here*
> //return rows;
>
> });
>
> con.end();
>
> //}
> //console.log('Outside function block: '+execQuery());
> return result; /*/ want to see here*
> }
>
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/3eaabb4b-94ee-41bd-ab00-71095dff5fce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.