0

i'm in hardhat i'v this public mapping

    mapping (address user => uint[] commentIds) public userComments;

and i'm trying to call it inside my test Script

        console.log(`user1 comments:  ${await postContract.userComments(user1.address)}`);

i get this error Error: no matching fragment (operation="fragment", info={ "args": [ "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" ], "key": "userComments" }, code=UNSUPPORTED_OPERATION, version=6.8.0)

**i can access to the mapping by create a getter function but why is that isn't that public modifier already do that under the hood **

1 Answer 1

1

If you are accessing a mapping that is some type to an array type, you need to pass the array index that you'd like to access as well.

For example: access first element in the returned list

console.log(`user1 comments:  ${await postContract.userComments(user1.address, 0)}`);

Why? well, if you check abi of your contract, the userComments will have two parameters. address, and uint (this is the index of returned list at given address)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.