1

I'm trying to write a simple function retrieving the contract address with a simple call. Anyway while the address value is correct in the function scope, the return value calling the function in that way is giving me an undefined error.

//Here the call

console.log("Indirizo con chiamata",App.getContractAddress());


//Here the function code


getContractAddress: function() {

RevenueShare.setProvider(web3.currentProvider);
  myContractInstance=RevenueShare.deployed();

myContractInstance.then(function(instance){
    return instance.address;
}).then(function(address){
  console.log("Indirizzo in funzione:",address);
  return address;
});
},

Any Help???

  • You are using truffle i think, but you are not catching any errors form the RevenueShare.deployed() call. if instance.address is undefined then there is a problem in the contract deployment. try to log instance.address and see what it gives. – Kaki Master Of Time Apr 5 '18 at 15:03
  • Yes I Am using truffle. Well actually instance.address is working, the console log inside the function gives out the right result (The contract address). Meanwhile when I try to call the function (as example in the first console.log) it returns an undefined. – Stefano Angieri Apr 5 '18 at 15:22
  • I it is just a javascript problem. promise returns are asyncronous so if you call your function, the result would be returned late and thus not shown. – Kaki Master Of Time Apr 5 '18 at 15:27
1

I think it is just a javascript problem. try this fix :

Function

getContractAddress: function() {
 return new Promise((res,rej)=>{
RevenueShare.setProvider(web3.currentProvider);
  myContractInstance=RevenueShare.deployed();

myContractInstance.then(function(instance){
    return instance.address;
}).then(function(address){
  console.log("Indirizzo in funzione:",address);
  res(address);
});

})

}

Function Call

App.getContractAddress()
.then(
    data=>{
        console.log("Indirizo con chiamata",data);
    })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.