2017-10-21 205 views
0

我正嘗試將合同連接到我的私有RPC服務器。我希望能夠更新我的html頁面上的計數器參數,這反過來又引用了我的瀏覽器上的solidity contract文件和更新。但是,我一直在遇到下面的錯誤爲我的HTML文件。 index.html和contract.sol也包含在下面。 TY!Web3回調函數問題 - Solidity

enter image description here 的index.html

<!doctype html> 
     <html> 
<head> 
    <title>myDapp</title> 
    <script src="web3.js/dist/web3.min.js"></script> 

    <script type="text/javascript"> 



     var contract_address = "0x68FDbd58D28BeD866E07906f6129bAC86161e243"; 
     var contract_abi = [ { "constant": true, "inputs": [], "name": "getCreator", "outputs": [ { "name": "", "type": "address", "value": "0xc0f0fb70a63e7b345932da8eb427463f586be95d" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "myNewNumber", "type": "uint256" } ], "name": "setMyNumber", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getMyNumber", "outputs": [ { "name": "", "type": "uint256", "value": "3" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" } ]; 

     if (typeof web3 !== 'undefined') { 
      web3 = new Web3(web3.currentProvider); 
     } else { 
      // set the provider you want from Web3.providers 
      web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 
     } 

     var contract_instance = web3.eth.contract(contract_abi).at(contract_address); 
     console.log(contract_instance); 


     function getCounter() { 
      document.getElementById("myCounter").innerText = contract_instance.getMyNumber().toNumber().call; 

} 


    </script> 
</head> 
<body> 
<h1>Interact with a contract</h1> 

<button onclick="getCounter()">Update Counter</button> 
<button onclick="increaseCounter()">Increase Counter</button> 

<span id="myCounter"></span> Counter 

</body> 
</html> 

contract.sol

pragma solidity ^0.4.15; 
contract MyContract { 
    address creator; 
    uint256 myNumber; 

    function MyContract() public { 
     creator = msg.sender; 
     myNumber = 3; 
    } 

    function getCreator() public constant returns(address) { 
     return creator; 
    } 

    function getMyNumber() public constant returns(uint256) { 
     return myNumber; 
    } 

    function setMyNumber(uint256 myNewNumber) public { 
     myNumber = myNewNumber; 
    } 

    function kill() public { 
     if(msg.sender == creator) { 
      selfdestruct(creator); 
     } 
    } 
} 
+1

[**不要發佈代碼或錯誤的圖像!**](https://meta.stackoverflow.com/q/303812/995714)圖像和屏幕截圖可以是一個很好的補充,但請如果沒有他們,那麼帖子仍然清晰有用。如果您發佈代碼圖片或錯誤消息,請確保您也直接複製並粘貼或將實際代碼/消息輸入到帖子中。 – Rob

回答

0

問題是你試圖直接與web3.js連接Metamask,是不是你應該怎麼做。 Metamask基於provider Engine和供應商eninge不支持同步功能或操作,例如:

你不能這樣做 web3.eth.accounts

,但你可以做web3.eth.getAcounts()

爲了最好地使用提供程序引擎來注入web3並從中取出它,您將注意到爲了使事情運行,您將切換到異步格式。

但在所有情況下使用web3.js異步功能,你會得到你想要的。