-1

I got stuck in getting the result return from the function for authentication the user.

utility.isAspAuth = function(token){
    queryString = {
        sql: 'SELECT * FROM SecurityToken WHERE Token = ?',
        timeout: 40000, // 40s
        values: [token]
    };
    var curTime = new Date();
    connection.query(queryString,function(err,rows,field){
        if(err){
            console.log('error on query' + err);
        }else{

            var result = rows[0];

            result.forEach(function(element, index, array){
                if(Date.parse(result.Expires) > curTime){
                    return true;
                }else{
                    return false;
                }
            });
        }
    });
};

so I call this function in another file, but the result is undefined

console.log(utility.isAspAuth(token))

can someone give me some hint on how to fix it?

Thank you so much

  • Async, async, async. Your `connection.query()` function is asynchronous. That means it calls it's callback sometime LATER, long after `isAspAuth()` has already returned. You cannot return an asynchronous value from a function like this. Instead, you must pass a callback into `isAspAuth()` and call that callback when you have your final value and you can only use the value from that callback. There are hundreds of dups of this type of question. I will go looking for one to mark this. – jfriend00 Sep 10 '15 at 22:23

1 Answers1

1

Issue is:

When you call utility.isAspAuth(token) from another file your function in utility file is getting execute but not returning anything at the time of call.

This is nature of nodejs. Notice in your code -

connection.query(queryString,function(err,rows,field){

only after query is done your callback function in above line gets executed, which wont' happen serially. It's asyn by default. This is why you're getting nothing ie. undefined in your console.log().

Fix is:

Use promises -

https://github.com/kriskowal/q

If a function cannot return a value or throw an exception without blocking, it can return a promise instead."

In your utility file -

utility.isAspAuth = function(token){

    var deferred = Q.defer();
    /* require('q') in your nodejs code.
       You see at the last line of this function we return a promise object.
       And in your success / fail function we either resolve/ reject the promise. 
       Simple. See the usage in other file where you log.
    */

    queryString = {
        sql: 'SELECT * FROM SecurityToken WHERE Token = ?',
        timeout: 40000, // 40s
        values: [token]
    };
    var curTime = new Date();
    connection.query(queryString,function(err,rows,field){
        if(err){
            // console.log('error on query' + err);

            deferred.reject(new Error(err));

        } else {

            var result = rows[0];

            result.forEach(function(element, index, array){
                if(Date.parse(result.Expires) > curTime){

                    deferred.resolve(true);

                }else{

                    deferred.resolve(false);
                }
            });
        }
    });


     return deferred.promise;

};

In your other file-

utility.isAspAuth(token)
.then(function (yourBoolResponse) {

    console.log(yourBoolResponse);
})
.catch(function (err) {

    // Handle error thrown here..
})
.done();
jitendra
  • 195
  • 1
  • 2
  • 10
  • Please don't post answers to questions for which there are many, many duplicates. Instead, just vote to close as a duplicate. This just pollutes the StackOverflow world if there are lots of duplicates all asking the same question. – jfriend00 Sep 10 '15 at 22:27
  • @jfriend00 - Yes, 'll keep this in mind from now onwards. Thanks btw for pointing. – jitendra Sep 10 '15 at 22:31