1

I'm trying to connect a smart contract to a very simply front end that has html/css/js. I keep getting an error when trying to use web3 to connect - I have the code below but the terminal is giving me an error of "web3.min.js:1 Uncaught TypeError: e.toLowerCase is not a function at Object.h [as inputAddressFormatter] (web3.min.js:1) at Object.set [as address] (web3.min.js:1) at o.e (web3.min.js:1) at new o (web3.min.js:1) at index.html:87" Here's the index.html:

<title>dApp Example</title>

<link rel="stylesheet" type="text/css" href="main.css">

<script src ="./dapp-eth/web3.min.js"></script>

Athletic Club Member?

    <label for="name" class="col-lg-2 control-label">Member name</label>
    <input id="name" class="text">

    <label for="name" class="col-lg-2 control-label">Member age</label>
    <input id="name" class="text">

    <button id="button">Update Instructor</button>

</div>

<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 {
        web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
    }

    //Set Account
    web3.eth.defaultAccount = web3.eth.accounts[0];

    //Set Contract Abi
    var contractAbi = [
{
    "constant": true,
    "inputs": [],
    "name": "getMember",
    "outputs": [
        {
            "name": "",
            "type": "uint256"
        },
        {
            "name": "",
            "type": "string"
        }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
},
{
    "constant": false,
    "inputs": [
        {
            "name": "_age",
            "type": "uint256"
        },
        {
            "name": "_name",
            "type": "string"
        }
    ],
    "name": "setMember",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
}

];

    //Set Contract Address
    var contractAddress = '0x0e53df94ff7b0e6c1977825b071f38a21248cbcd';

    //Set Contract
     new web3.eth.Contract(contractAbi,[contractAddress]);

    //var memberLookupContract = new web3.eth.Contract([{"constant":true,"inputs":[],"name":"getMember","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_age","type":"uint256"},{"name":"_name","type":"string"}],"name":"setMember","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}], '0x0e53df94ff7b0e6c1977825b071f38a21248cbcd');

    contract.getMember(function(error, result) {
    if(!error) {
        $("#member").html(result[0]+' ('+result[1]+' years old)');
    } else
    console.log(error);
});

$("#button").click(function() {
    contract.setMember($("#name").val(), $("#age").val());
    $("#loader").show();
});

</script>

** Line 87 is: new web3.eth.Contract(contractAbi,[contractAddress]);

1
  • You're passing the contract address as an array with one element. Instead, you should pass it as an individual element like: new web3.eth.Contract(contractAbi,contractAddress) Commented Jun 10, 2021 at 6:05

1 Answer 1

0

The correct way of setting contract:

//Set Contract
new web3.eth.Contract(contractAbi, contractAddress);

Another way you can try is:

var contractAbi = web3.eth.contract('YOUR ABI')  
var mycontract = contractAbi.at('YOUR CONTRACT ADDRESS')

Now calling contract functions using mycontract.

mycontract.getMember(function(error, result) {
    if(!error) {
        $("#member").html(result[0]+' ('+result[1]+' years old)');
    } else
    console.log(error);
});
2
  • Neither is working for me - the '2nd' way of trying this as you advised is returning the error of "web3.eth.contract is not a function".
    – Carlos85
    Commented Aug 15, 2019 at 3:06
  • Set the web3 version to 0.20.0. You can install the same using the command npm install ethereumjs-testrpc [email protected]. You can also check the version currently installed using npm ls web3 . web3.eth.contract will now work. Commented Aug 16, 2019 at 17:00

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.