Skip to content

Issue in calling a smart contract function with web3

I’m trying to call the createCustomer function provided in this smart contract

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(
            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) });
    }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!



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