0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but how to get info I need I have set but I can't get result I wanna
the callback to checkTokenIsExpired takes 2 parameters, so when you call it, you need to pass a value as the second parameter when calling the response function (the name of that callback in checkTokenIsExpired). That's why it undefined.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.