I am learning nodejs and stuck . Need your help. When I run function getSessionKey() function... I am able to log session key inside request call back but not able to log it outside request. Please help me how can access it outside.
function getSessionKey(){
var key = '';
var tempObj = {method:'',id:0,params:{username:'',password:''}};
tempObj.id = 1;
tempObj.params.username = 'test';
tempObj.params.password = 'test';
tempObj.method = 'get_session_key';
options.body = JSON.stringify(tempObj);
request(options, function (error, response, body) {
if (error) throw new Error(error);
var keyObj = JSON.parse(body);
key = keyObj.result;
console.log(key); //here I am able to access key
});
//console.log(key); //but I want to get it here.
}
getSessionKey()