Skip to content
Advertisement

Issue in calling a smart contract function with web3

I’m trying to call the createCustomer function provided in this smart contract https://ropsten.etherscan.io/address/0xD3B462CbF6244ed21CD3cF334Bf8CB44A28795A9#code

and we basically have to provide three parameters like string memory _hashedEmail, string memory _name and string memory _phone.

So I’ve written the following program to call the createCustomer function

const addcustomer = async (req, res, next) => {
  try {
        const init = async() => {
            const provider = new  HDWalletProvider(
              privateKey1,
              'https://ropsten.infura.io/v3/1693cef23bd542968df2435f25726d39'
            );
            const web3 = new Web3(provider);

            let contract = new web3.eth.Contract(abi2, address3);
            contract.methods.createCustomer({_hashedemail: "a", _name: "nike", _phone: "99"}).call((err, result) => { console.log(result) });
            };
            init(); 
    }catch (err) {
        //throw error in json response with status 500. 
        return apiResponse.ErrorResponse(res, err);
    }
};

However it gives me this err which doesnt make any sense as i’ve already provided the three paramaters.

(node:14744) UnhandledPromiseRejectionWarning: Error: Invalid number of parameters for "createCustomer". Got 1 expected 3!

Advertisement

Answer

Remove the { } inside createCustomer. So to be clear, this line:

contract.methods.createCustomer({_hashedemail: "a", _name: "nike", _phone: "99"})

should be:

contract.methods.createCustomer(_hashedemail: "a", _name: "nike", _phone: "99")

plus, when you try to interact with a function that modify the blockchain you shouldn’t call it using .call but instead with .send.

To learn more you should check the web3.js docs

Advertisement