Web3js

web3.eth.contract 不工作

  • November 11, 2019

我使用 npm 安裝了 web3@0.20.2 。當我呼叫 web3.eth.contract() 時,它說它不是一個函式,而當我使用 new web3.eth.contract 時,它說它既不是建構子。這是我的程式碼

<!DOCTYPE html>
<head>
   <script src="./node_modules/web3/dist/web3.min.js">
   </script>
</head>

<body>
<input name="choice" type="text"> Vote: </input>
<button id="submit">Submit : </button>
</body>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script>

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

// Previous if/else statement removed for brevity

web3.eth.defaultAccount = web3.eth.accounts[0];
var vote=  web3.eth.contract([
   {
       "constant": true,
       "inputs": [],
       "name": "c4",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "c2",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "c1",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": false,
       "inputs": [
           {
               "internalType": "uint256",
               "name": "x",
               "type": "uint256"
           }
       ],
       "name": "set",
       "outputs": [],
       "payable": false,
       "stateMutability": "nonpayable",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "c0",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "get",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           },
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           },
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           },
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           },
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           },
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "c5",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   },
   {
       "constant": true,
       "inputs": [],
       "name": "c3",
       "outputs": [
           {
               "internalType": "uint256",
               "name": "",
               "type": "uint256"
           }
       ],
       "payable": false,
       "stateMutability": "view",
       "type": "function"
   }
],'0x1def5958505131be74680211266cd0667b3fc75d');

console.log(vote);


</script>
</html>

請問有人能找到錯誤嗎?

對於 web v0.20,創建對現有實例的引用應該分兩步完成

// Create contract object
var MyContract = web3.eth.contract(abiArray);

// Reference instance
var contractInstance = MyContract.at(address);

我相信您實際上是在 web3 v1.x 上,在這種情況下,您確實應該使用new web3.eth.Contract,但使用大寫的C.

為了驗證您使用的是哪個版本的 web3,請npm ls web3從命令行或console.log(web3.version)JS 程式碼執行。

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