All of the posts I've seen say that Solidity doesn't let you return an array struct, but when I do the following, no error is thrown and I can, in fact, get the array of struct in the front end:

pragma solidity >=0.7.0 <0.9.0;

struct SomeStruct {
   string name;   
   uint number;

mapping(address => SomeStruct[]) database

function getAll() public view returns (SomeStruct[] memory) {
     SomeStruct[] memory structArray = database[msg.sender];
     require(structArray.length > 0, "is empty");
     return structArray;

Is the newer version of Solidity allowing this?

The reason I'm investigating this is because the returned array is occasionally missing an element and I can't seem to pinpoint whether this is caused by above code or not.

** Update **

mapping (address => File[]) database;
mapping (address => uint) public index;

struct File {
    string hashString;
    string date;
    bool isActive;
    uint index;
    uint32 size; 
    string name;   
function setFile(string memory _hashString, string memory date, uint32 _size, string memory _name) public {
    uint fileIndex = index[msg.sender];
    File memory newFile;
    newFile.hashString = _hashString;
    newFile.date = date;
    newFile.isActive = true;
    newFile.index = fileIndex;
    newFile.size = _size;
    newFile.name = _name;
    fileIndex ++;
    index[msg.sender] = fileIndex;

function getAllFiles() public view returns (File[] memory) {
    File[] memory files = database[msg.sender];
    require(files.length > 0, "There are currently no files");
    return files;
  • It should work in solidity v0.8. It was possible in previous versions using pragma experimental ABIEncoderV2. If something doesn't work try creating reproducible example.
    – Ismael
    Apr 3 at 2:23
  • @Ismael I included the reproducible example. To summarize, whenever I upload an image to IPFS and get a hash value back, I'm able to upload that hash to the Ethereum network with setFile and download it with getAllFiles perfectly fine. When I upload a document like PDF to IPFS, I also get a hash value back with no issues and am able to upload it to the Ethereum network as well. However, when I execute getAllFiles, only the File structs are fetched 100% of the time and the document File struct maybe 10% of the time.
    – Kevvv
    Apr 3 at 22:47
  • The structs for PDF are simply missing from the returned array. I'm very confused about this because I know for sure that the IPFS has been uploaded properly since I get back the legitimate hash value so there's no issues there. I also know that setFile is executed property because I get back the transaction details like the hash, gas price, etc with no error. When I test the above code in Truffle and Remix, there is no issue.
    – Kevvv
    Apr 3 at 22:48
  • I tried your contract, there's no obvious issues and it works as expected. The problem might be on the client side. Perhaps there are too many files? Or some event is fired incorrectly. The solidity seems to be correct.
    – Ismael
    Apr 5 at 0:07
  • thanks for giving it a try. If there are too many files, such a problem could exist?
    – Kevvv
    Apr 5 at 0:39

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.