0

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 ?

0

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

I believe that you're actually on web3 v1.x, in which case, you should indeed be using new web3.eth.Contract, but with a capital C.

In order to verify which version of web3 you're on, run npm ls web3 from a command line or console.log(web3.version) from your JS code.

  • Done ! Not working constructor is used in web3@1.2.x or 1.0.0 ! it was used as a function in 0.20.6 ! Am i right ? – Somil gupta Nov 11 at 14:21
  • @Somilgupta: See updated answer. – goodvibration Nov 11 at 14:22
  • PS C:\Users\somil\attempt> npm ls web3 attempt@1.0.0 C:\Users\somil\attempt `-- web3@0.20.2 – Somil gupta Nov 11 at 14:26
  • @Somilgupta: Are you trying to tell me something here? Because if you do, then it is unreadable! – goodvibration Nov 11 at 14:27
  • i was just trying to tell you its web3@0.20.2 . But i ll try as constructor too tysm !! if it doesnt work i ll get back to you ! – Somil gupta Nov 11 at 14:29
0

If you are using Web3@0.2x.x, you can look at API Documentation how to create a contract object and initiate it on an address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.