I would like to check if a specific address is included in an address key mapping from web3?

Here is my Solidity Smart Contract:

pragma solidity >=0.4.21 <0.7.0;

contract Election {

    //Model Candidate
    struct Candidate {
        uint id;
        string name;
        uint voteCount;

    mapping(uint => Candidate) public candidates;
    //store accounts that have voted
    mapping(address => bool) public voters;

    uint public candidatesCount;

    event votedEvent (
        uint indexed _candidateId

    constructor() public {
        addCandidate("Barack Obama");
        addCandidate("Donald Trump");

    function addCandidate(string memory _name) private {
        candidatesCount ++;
        candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);

    function vote(uint _candidateId) public {
        //not voted yet

        //valid vandidate
        require(_candidateId > 0 && _candidateId <= candidatesCount);

        //record vote
        voters[msg.sender] = true;

        //update candidate vote count
        candidates[_candidateId].voteCount ++;

        //voted event
        emit votedEvent(_candidateId);


Actually, I would like to disable the voting button from my front-end if the connected address (with Metamask) already voted for a candidate.

I tried something like this from my Front-end:

const alreadyVoted = await contract.methods.voters('USER_ADDRESS').call({
        from: 'USER_ADDRESS'

But I got the following error:

"Error: invalid address (argument="address", value=undefined, code=INVALID_ARGUMENT, version=address/5.4.0) (argument=null, value=undefined, code=INVALID_ARGUMENT, version=abi/5.0.7)"

Thanks for your help!

  • 1
    From the error message it appears you are passing a variable with undefined. Make sure the parameter to voters it is an address.
    – Ismael
    Aug 31 '21 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.