簡體   English   中英

Web3功能的回撥問題-牢固性

[英]Web3 Call Back Issue for Function - Solidity

我正在嘗試將合同連接到我的私有RPC服務器。 我希望能夠在我的html頁面上更新一個計數器參數,該參數繼而引用我的團結合同文件並在我的瀏覽器上進行更新。 但是,我不斷遇到下面的html文件的關注錯誤。 index.html和contract.sol也包括在下面。 TY!

在此處輸入圖片說明 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>

合同

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

問題是您試圖將Metamask直接與web3.js連接,而不是應該這樣做。 元掩碼基於提供程序引擎 ,提供程序inge不支持同步功能或操作,例如:

你不能做web3.eth.accounts

但您可以執行web3.eth.getAcounts()

為了最好地工作,最好使用提供程序引擎注入web3並從那里獲取它,您將注意到如何使事情運行,將所有內容切換為異步格式。

但在所有情況下都使用web3.js異步函數,您將獲得所需的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM