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???

3
  • 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. Commented Apr 5, 2018 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. Commented Apr 5, 2018 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. Commented Apr 5, 2018 at 15:27

1 Answer 1

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);
    })
0

Your Answer

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

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