giaosudau
giaosudau

Reputation: 2251

Callback function in Nodejs

I have function

var checkTokenIsExpired = function(name, token, response) {
LoginToken.find( { name: name, series: token }, function(error, info){
                console.log("info: ", info[0]['expire']);
                if (error) {
                    // response.send("error: %s}", error);
                    response(error);
                }
                if (info.length > 0) {      
                    var expire = new String(info[0]['expire']);
                    // expire = expire.substr(0,26);    
                    var date = new Date(expire);
                    if (date >= new Date()) {                       
                        // response.send("{info: success" );
                        response("success");
                    }
                    else{
                        // response.send("error: token-has-expired");
                        response("token-has-expired");
                    }   
                    console.log("Response: ", info);
                }
                else {
                    response(null);
                }
            } );
}

To check token is expired or not? It will return a string.

And I call this function in here

    exports.updateAccount = function(newData, callback)
{
    Accounts.find({name:newData.body.name}, function(err, docs){
        if (err) {
            callback.send('{error: ' + String(err) + "}");
        }
        if (docs.length==0 || !docs) {
            callback.send("{error: NULL }");
        }
        else {
            checkTokenIsExpired(newData.body.name, newData.body.token, function(error, info){
            if (error){
                        callback.send("{error: " + error + "}");
            // I want to get info here      }               
            console.log("check token: ", info);
            // check info of token is expired or not
            if (info!=null && info!="token-has-expired") {
                var updateString = "";
                if (newData.body.screen_name){
                    Accounts.update( {'name': newData.body.name}, 
                            {
                                "screen_name" : newData.body.screen_name
                            },
                            { 'upsert' : true },
                            function (err, numberAffected, raw) {
                              if (err) return handleError(err);

                            });
                }
                if (newData.body.email){
                    Accounts.update( {'name': newData.body.name}, 
                            {                               
                                "email": newData.body.email
                            },
                            { 'upsert' : true },
                            function (err, numberAffected, raw) {
                              if (err) return handleError(err);

                            });
                }
                if (newData.body.password == ''){


                }   else{
                    var password = encodePassword(newData.body.password, docs[0]['salt']);                  
                    Accounts.update( {'name': newData.body.name}, 
                            {
                                "hassedPass" : password,                                
                            },
                            { 'upsert' : true },
                            function (err, numberAffected, raw) {
                              if (err) return handleError(err);

                            });

                }               


            }
        });

        }

I want to get the info when call checkTokenIsExpired but when I console.log("info", info) it return undefined.

Upvotes: 0

Views: 814

Answers (2)

giaosudau
giaosudau

Reputation: 2251

I have solved problem

checkTokenIsExpired(newData.body.name, newData.body.token, function(error, info)

because it will take 1 parameter but I put 2 Change it into checkTokenIsExpired(newData.body.name, newData.body.token, function(info) It will correct :D

Upvotes: 0

Pascal Belloncle
Pascal Belloncle

Reputation: 11389

in checkTokenIsExpired, you need to pass info to the response callback otherwise it is not in scope of your second code.

at least in the case where you have success:

response("success", info);

Instead of passing "success", you'd typically pass null to indicate there is no error.

Upvotes: 1

Related Questions