0

I am using requsts package to call api from another server. But the global variable outside the function is not getting updated and showing undefined.

var request = require('request');
var contractAddress;
var abi;

request('http://expressanthem:9000/getcontractAddress',function(error,response,body){
    contractAddress=body;
    console.log(contractAddress+" "+"contract address at first");
});

request('http://expressanthem:9000/getcontractAbi',function(error,response,body1){
    // contract=body1;
    // console.log(typeof contract);
    abi=body1;
    console.log(abi+" "+"This is the abi at first");
}); 
console.log("The contractAddress is"+" "+contractAddress);
console.log("The cotractABI is"+" "+abi);

module.exports={
   contractAddress,
   abi
};

The global varibale ABI and contract address is giving values undefined.

Jithin Zacharia
  • 33
  • 1
  • 1
  • 8
  • Please read on aync nature of Node.js and callbacks in javascript. You will get your answer. – XCEPTION Apr 16 '18 at 12:38
  • request is asynchronous. The problem here is that you try to log your result before the querry is finished. To handle asynchronous operation I recommend to read about promise or async / await – Alex Mass Apr 16 '18 at 13:34

0 Answers0