solidity
solidity copied to clipboard
Mapping:The getter automatically generated by the mapping has a bug.
Description
The getter automatically generated by the mapping has a bug.
Environment
Remix 0.8.29
Steps to Reproduce
When I use " mapping(address => uint256[]) public holderCertificates;",the getters automatically generated by the mapping do not support getting arrays by address.
I have to manually construct the getter to retrieve the data.
Hi @Un96, thanks for the follow‑up—what you’re seeing is actually by design, not a bug:
Auto‑getter signature
mapping(address => uint256[]) public holderCertificates;
the compiler only emits:
function holderCertificates(address _holder, uint256 _idx) external view returns (uint256);
i.e. you must supply both the holder’s address and an array index to fetch a single element. There is no holderCertificates(address) or length getter.
How to expose length or the full array
If you need to read the array length or return the entire list, add custom functions, for example:
/// @notice Get the number of certificates for a holder
function getHolderCertificatesLength(address _holder) external view returns (uint256) {
return holderCertificates[_holder].length;
}
/// @notice Get all certificate IDs for a holder
function getHolderCertificates(address _holder) external view returns (uint256[] memory) {
return holderCertificates[_holder];
}