Javascript

Javascritp app.js 函式返回未定義

  • April 5, 2018

我正在嘗試編寫一個簡單的函式,通過一個簡單的呼叫來檢索契約地址。無論如何,雖然地址值在函式範圍內是正確的,但以這種方式呼叫函式的返回值卻給了我一個未定義的錯誤。

//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;
});
},

任何幫助???

我認為這只是一個javascript問題。試試這個修復:

功能

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

})

}

函式呼叫

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

引用自:https://ethereum.stackexchange.com/questions/44840