0

I want to return an array of structs because i want to output all my data.

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.0;

contract MyContract
{
    mapping(uint256 => People) dataList;
    uint256[] countArr;
    struct People{
        string name;
        uint256 favNum;
    }

In this function, i set the data of my struct object and then include it in my mapping.

    function setPerson(string memory _name,uint256 _id, uint256 _favNum) public {
        dataList[_id]= People(_name,_favNum);
        countArr.push(_id);
    }

This function here gets me the data of my specified struct object.

    function getPerson(uint _id) public view returns(string memory,uint256){
        return (dataList[_id].name, dataList[_id].favNum);
    }

Now here is the function i think that is causing me trouble because in this function i want to return not the data of a single People object but all of my data and whenever i run this function on my REMIX IDE console it shows me the error: call to MyContract.getAllData errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance.

    function getAllData()public view returns(People[] memory){
        uint256 count = (countArr.length);
        uint256 i = 0;
        People[] memory outputL= new People[](count);
        while(count >= 0){
            (string memory nam,uint256 num) = getPerson(count-1);
            People memory temp = People(nam,num);
            outputL[i]=temp;
            count--;
            i++;
        }
        return outputL;
    }                
}

Can anyone help and explain what is wrong and how can i get it running?

2 Answers 2

0

Your issue is in the logic in while() condition and array length. In your original smart contract, you can use count variable for track the array length minus 1 but this last operation is wrong. Because the length about array don't start from 0. Example: If I have this array:

[0] = element

[1] = element

[2] = element

array_length is 3

Because of this issue, if in people array there are 3 elements, count variable has value: 2 and when you initialize the outputL array, the dynamic length about this last object is 2 and this is wrong! For this reason, you receive that error.

I adjusted your smart contract, and you can see it in the following lines:

//SPDX-License-Identifier:MIT
pragma solidity ^0.8.0;

contract MyContract
{
    mapping(uint256 => People) dataList;
    uint256[] countArr;
    
    struct People{
        string name;
        uint256 favNum;
    }

    function setPerson(string memory _name,uint256 _id, uint256 _favNum) public {
        dataList[_id]= People(_name,_favNum);
        countArr.push(_id);
    }

    function getPerson(uint _id) public view returns(string memory,uint256){
        return (dataList[_id].name, dataList[_id].favNum);
    }

    function getAllData()public view returns(People[] memory){
        uint256 count = countArr.length;
        uint256 i = 0;
        People[] memory outputL = new People[](count);
        while(count > 0){
            (string memory nam,uint256 num) = getPerson(i);
            People memory temp = People(nam,num);
            outputL[i]=temp;
            count--;
            i++;
        }
        return outputL;
    }                
}
2
  • Yes so sorry i saw that after posting but unfortunately this still gives me the same error anything else you might suggest really appreciate your help Commented Jul 14, 2022 at 15:40
  • hey sorry for previous comment your solution worked,Thanks alot. Commented Jul 15, 2022 at 16:13
0

The change mentioned by @Kerry99 is correct. I tested it and can confirm that. Seems you have not deployed the contract with the change.

1
  • yeah i was messing up the while loop condition. Commented Jul 15, 2022 at 16:12

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.