3
votes

This is my contract code. Here I'm trying to store the coordinates of a particular trip. While storing the information contract executing fine. But When I retrieve the data, It should give the array of coordinates. But it is throwing an error.

reason: 'insufficient data for uint256 type'

contract TripHistory {
       struct Trip {
           string lat;
           string lon;
       }
        mapping(string => Trip[]) trips;

        function getTrip(string _trip_id) public view returns (Trip[]) {
            return trips[_trip_id];
        }
        function storeTrip(string _trip_id, string _lat, string _lon) public  {
           trips[_trip_id].push(Trip(_lat, _lon));
        }

}

What I'm missing here. Is there any other way to achieve what I'm trying here?

P.S: I'm new to solidity.

2

2 Answers

6
votes

First of returning structs is not supported in Solidity directly. Instead you need to return every individual element in the struct as below.

Function xyz(uint256 _value) returns(uint256 User.x, uint256 User.y)
public {}

But then there’s an experimental feature that will help you with returning struct. All that you need to do is add the following after your first pragma line

pragma experimental ABIEncoderV2;

then continue with your code. That should work with no changes to your code.

An example of abiencoderv2 returning struct can be found at this link

1
votes

It is not possible in solidity to return struct array.