web3js – web3.eth.contract Not Working – How to Fix Common Issues

blockchainnodejsprivate-blockchainweb3js

I have web3@0.20.2 installed using npm . when I call web3.eth.contract() it says that it is not a function and when I use new web3.eth.contract it says it is neither a constructor. This is my code

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

Please can someoneone find error in it ?

Best Answer

For web v0.20 creating a reference to an existing instance should be done in two steps

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

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