0

I have [email protected] 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 ?

3 Answers 3

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

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.

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

If you are using [email protected], 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 and acknowledge you have read our privacy policy.

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