Use of global variable in node js

654 views
Skip to first unread message

SURAJ KUMAR CHANDRA

unread,
Mar 27, 2017, 8:28:47 PM3/27/17
to nodejs
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
}

Howard Dierking

unread,
Mar 28, 2017, 3:59:51 PM3/28/17
to nodejs
Based on a quick glance, it appears that the problem here has less to do with variable scope and more to do with your use of an async function (e.g. con.query). In the above, your function has likely exited before the callback has executed, meaning that result never has a chance to be set. Further, even if your callback did manage to execute, you can't just return it as though it's in the outer function's scope. You would need to execute a callback on the outer function or return a promise.

hth,

_howard

Bruno Jouhier

unread,
Mar 28, 2017, 3:59:57 PM3/28/17
to nodejs
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) 

SURAJ KUMAR CHANDRA

unread,
Mar 28, 2017, 9:54:25 PM3/28/17
to nodejs
Hey Thanks Bruno,
Can you please alter this code? I have been fighting with user defined callback.

Julia Y

unread,
Mar 29, 2017, 3:00:20 PM3/29/17
to nodejs
Hi there, I don't know, will it be interesting for you, but think that yes - Here's the research of The best Node.js framework for your project between Express.js, Koa.js and Sails.js published by the development company: https://www.cleveroad.com/blog/the-best-node-js-framework-for-your-project--express-js--koa-js-or-sails-js

SURAJ KUMAR CHANDRA

unread,
Mar 29, 2017, 4:02:34 PM3/29/17
to nodejs
Thanks mate. But I am still finding it difficult to convert my above logic to make it async and return login data. I think I need to use multiple callback.

endl...@gmail.com

unread,
Apr 20, 2017, 3:17:51 PM4/20/17
to nodejs
function login(email, password, callback){

    var email = email.trim().toLowerCase();
    var password = password.trim();

    var queryString = "SELECT * FROM users where Email ="+"'"+email+"' AND Password = '"+password+"'";

    con.query(queryString, function(err, rows){
        if(err){
            throw err;
        }
        callback(rows);
    });
};

login('skc...@gmail.com', 'Password', function(results){
    con.end();
    // Do stuff with results
});

tpx1

unread,
Apr 21, 2017, 12:13:41 AM4/21/17
to nod...@googlegroups.com
Hi,

I will mention that this piece of code has a big security issue.
You should never combine a sql query string with your parameter
directly. If email is a string like


' OR 1=1 --

you will login without any login information. For more information
see https://en.wikipedia.org/wiki/SQL_injection.

Use a sql library that supports sql prepared statements.

Another problem is that it seems so, that you store the password in
plain. You shouldn't do this in fact of security issues. Store passwords
as a hash and before querying the database, you hash the given password
two.

This post explains, how to do it in node.
http://lollyrock.com/articles/nodejs-sha512/

Thomas

Am 19.04.2017 um 13:59 schrieb endl...@gmail.com:
> function login(email, password, callback){
> var email = email.trim().toLowerCase();
> var password = password.trim();
>
> var queryString = "SELECT * FROM users where Email ="+"'"+email+"'
> AND Password = '"+password+"'";
>
> con.query(queryString, function(err, rows){
> if(err){
> throw err;
> }
> callback(rows);
> });
> };
>
> login('skc...@gmail.com', 'Password', function(results){
> con.end();
> // Do stuff with results
> });
>
> On Tuesday, March 28, 2017 at 5:58:47 AM UTC+5:30, 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 nodejs+un...@googlegroups.com
> <mailto:nodejs+un...@googlegroups.com>.
> To post to this group, send email to nod...@googlegroups.com
> <mailto:nod...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/c4e339af-c4cb-4e47-8a56-3162bb079789%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/c4e339af-c4cb-4e47-8a56-3162bb079789%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages