I am designing an EHR System that will store patient records (with a unique ID) and separated by the date and Hospital entered. I am able to enter data in Hospital struct mapping to the Record struct, and everything is working well.

contract PatCord {
string  name;
uint  age;
string  bGroup;
bytes32  id;
mapping(address => string) hosp;
address recc;

constructor(string _name, uint _age, string _bGroup, bytes32 _id) public {
    name = _name;
    age = _age;
    bGroup = _bGroup;
    id = _id;
    hosp[msg.sender]= "ABC Hospital";

struct Record {
    string disease;
    string value;

struct Hospital {
    string name;
    Record[] records;

mapping(uint256 => Hospital) hos;
Hospital[] hospital;

function addRecord(uint _id, string _disease, string _value) public {
    hos[_id].records.push(Record(_disease, _value));

function returnRecord(uint _id) public view returns(string ,  string[] memory, string[] memory) {
    string memory name = "ABC Hospital";
    uint256 length = hos[_id].records.length;
    string[] memory disease = new string[](length);
    string[] memory value = new string[](length);
    for (uint i = 0; i < length; i+=1) {
        disease[i] = hos[_id].records[i].disease;
        value[i] = hos[_id].records[i].value;
    return (name , disease, value);

function allRecords() view returns (Hospital[]) {
    return hospital;


I want to store every Hospital record inside an array which I can show separately on the screen. But finding no solution to this problem. Can anyone help me on this?

  • What is the problem with your own solution? – goodvibration Oct 16 '20 at 8:29
  • It's almost right. The records are getting stored perfectly. But I want to store all the Hospital records in an array, so I can show them on the front end (One by One). – Shubham Sharma Oct 16 '20 at 8:34
  • are you gonna use that in a public blockchain?? you are aware that everything will be public and the name of people and their disease will be accessible for everyone? – Majd TL Oct 16 '20 at 8:47
  • It's a dummy project, just for practice. Besides that, I have not sent the full code here. The user will get entry into the contract iff, 1. He is a verified patient, 2. msg.sender is registered with the contract. So I guess it won't be accessible that easily. – Shubham Sharma Oct 16 '20 at 9:01

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.