1

I'm declaring a public address array in my contract, but when I call it, I get an error saying it's expecting an argument. Can someone shine a light on what is happening here?

Contract.sol

uint public buyIn;
address[] public playersRegistered;

index.js

// this works
console.log(await contract.methods.buyIn.call());

// this throws an error
console.log(await contract.methods.playersRegistered.call());

Error message: Unhandled Rejection (Error): The number of arguments is not matching the methods required number. You need to pass 1 arguments.

1

It is true that solidity creates getters for public variables but if the variables are referenced variables it does not return the value as expected. Instead you need to pass the index for the array to get the value of specific index. To get the complete array elements you need to write the getter for array. let me show you the contract for this:

pragma solidity ^0.5.5;
contract Test {
   uint [] public players ;
    constructor() public {
        players.push(1);
        players.push(2);
        players.push(3);
    }

    function getArray() public view returns (uint[] memory){
       return players;
    }
}

So when you will try to call the getter for public array variable from web3 without giving the index it will throw an error because argument for function call will not match. See below the running example of the above contract to under more.

enter image description here

Hope you get your answer.

1

Solidity creates getters for public variables and requires an index for arrays. Answer found here: https://ethereum.stackexchange.com/a/60491/48494

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.