0

need a lil help here in this contract i have used nested mappings where i've mapped an address with another mapping which stores date and disease, basically i want to get all the dates when patient has visited the hospital

pragma solidity ^0.6.6;

contract Mycontract { mapping(address => mapping(string=>string)) public patient; function addPatientrecord(string memory _date,string memory _disease) public { patient[msg.sender][_date] = _disease; } }

1
  • pragma solidity ^0.6.6; contract Mycontract { mapping(address => mapping(string=>string)) public patient; function addPatientrecord(string memory _date,string memory _disease) public { patient[msg.sender][_date] = _disease; } }
    – Mohsin
    Nov 6, 2021 at 12:25

1 Answer 1

1

Date itself is considered a uint and not a string as it is represented in unix timestamp https://www.unixtimestamp.com/. In order to achieve what you want, your mapping should look like this:

mapping(address=>mapping(uint=>string)) 

And your function parameter _date should be a uint:

 function addPatientrecord(uint _date,string memory _disease) public{}

Also, the patient record as far as I see will only be accessed by a hospital employee, so the msg.sender will be the employee, not the patient itself, hence imo the address should be replaced with an ID of the patient to keep track of the records of the patient with that specific ID. This can be achieved as follows:

mapping(uint=> mapping(uint=>string) 

UPDATE

To access multiple details of a record based on a key, you can use a struct.

struct PatientRecord{
uint ID;
uint date;
string disease;
}

and then map that structure to a key which in your case would be an address, although I think the ID would make more sense. For that the mapping would be as follows:

For address:

mapping(address => PatientRecord[]) pAddressToRecord;

and push a PatientRecord struct into it:

PatientRecord memory record;
pAddressToRecord[msg.sender].push(record);

after which you retrieve it with a view function:

function getPatientRecords(address patientAddress) public view returns(PatientRecord[] memory){

return pAddressToRecord[patientAddress];//here you will get all the records for that specific address
}

The same logic goes with the ID instead of address. It depends a lot of what you want to achieve. Let me know if it works.

4
  • the problem here is for retrieving record i have to give address and date , what i want is that i just give an address and get all the mappings against it eg input : addr , output : { 27-04-2020 , "flew "} , { 25-07-2021 , "corona" } .....
    – Mohsin
    Nov 6, 2021 at 15:35
  • I've edited the answer. Let me know if it works.
    – Icarus23
    Nov 6, 2021 at 17:01
  • love you bro just in your updated code i have to add pragma experimental ABIEncoderV2; and in get function i have to add view returns ( PatientRecord[])
    – Mohsin
    Nov 6, 2021 at 19:09
  • yes, correct! I forgot that the return type was an array. It's edited now. Glad it worked out!
    – Icarus23
    Nov 6, 2021 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.