Error when fetching pending voters from smart contract using ethers.js

14 Views Asked by At

I am developing a voting application using React.js, ethers.js, and a Solidity smart contract. The application allows users to register as voters, and the admin can approve or reject pending voters. However, when I try to fetch the list of pending voters from the smart contract, I encounter an error in the console:

PendingVotersPage.js:22 Failed to fetch pending voters: Error: no matching fragment (operation="fragment", info={ "args": [], "key": "pendingVoters" }, code=UNSUPPORTED_OPERATION, version=6.11.1) at makeError (errors.ts:694:1) at assert (errors.ts:715:1) at getFragment (contract.ts:270:1) at Proxy.pendingVoters (contract.ts:350:1) at VotingService.callContractMethod (VotingService.js:39:1) at async fetchPendingVoters (PendingVotersPage.js:16:1)

PendingVotersPage.js

const fetchPendingVoters = async () => {
    try {
        const voterAddresses = await VotingService.getPendingVoters();
        const voterDetails = await Promise.all(
            voterAddresses.map((address) => VotingService.getVoter(address))
        );
        setPendingVoters(voterDetails);
    } catch (error) {
        console.error('Failed to fetch pending voters:', error);
    }
};

VotingService.js

async getPendingVoters() {
    return this.callContractMethod('pendingVoters');
}

Solidity.sol

address[] public pendingVoters;

function getPendingVoters() public view returns (address[] memory) {
    return pendingVoters;
}

Steps to reproduce the problem: Deploy the smart contract Register some voters using the registerVoter function Navigate to the PendingVotersPage component in the React application Observe the error in the browser console Expected behavior: The PendingVotersPage should fetch the list of pending voters from the smart contract and display their details in a table. Actual behavior: The application fails to fetch the pending voters and throws the mentioned error in the console. Environment: React.js version: 18.2.0 ethers.js version: 6.11.1 Solidity version: 0.8.24 Browser: Google Chrome I have tried to debug the issue by adding console logs and breakpoints, but I couldn't figure out the root cause of the problem. I would appreciate any help or guidance in resolving this issue and successfully fetching the pending voters from the smart contract.

0

There are 0 best solutions below