I have the following Solidity code that compiles and tests okay, but the insurancePlan property of the Patient struct is not being saved. I've tried many variations and it is just blank (empty) when I go to access the property. Any help would be appreciated.

contract Prescription {
    // Define patient struct
    struct Patient {
        address patient;
        bool insured;
        string insurancePlan;
        Rx[] prescriptions;
    // Define prescription struct
    struct Rx {
        uint cost;
        string code;
        string description;
        uint quantity;
        bool active;
        bool refill;

    Patient[] public patients;

    * Create a new patient        *
    function newPatient(address _patient, bool _insured, string memory _insurancePlan) external {   
        Patient storage patient = patients.push();
        patient.patient = _patient;
        patient.insured = _insured;
        patient.insurancePlan = _insurancePlan;

        Rx storage prescription = patient.prescriptions.push();
        prescription.cost = 0;
        prescription.code = "none";
        prescription.description = "none";
        prescription.quantity = 0;
        prescription.active = false;
        prescription.refill = false;

I am then accessing the information using the following method:

    * Return a patient            *
    function getPatient(address _patient) 
        returns(address patient, bool insured, string memory insurancePlan, Rx[] memory prescriptions) 
            for(uint i = 0; i < patients.length; i++) {
                if(patients[i].patient == _patient) {
                    return (patients[i].patient, patients[i].insured, patients[i].insurancePlan, patients[i].prescriptions);
  • I tried your code (pragma 0.7.0) in Remix and patient, insured & insurancePlan are correctly saved in struct Patient. Perhaps it is the way you are accessing this variable? (from web3.js, etc) Mar 21, 2021 at 18:51
  • Everything looks good as I step through the processing. I'm calling the contract method using this line when creating the transaction: 'data': presContract.methods.newPatient(wallet, insured, insurance).encodeABI() Maybe this is not the correct way to use this method? I'm new to Solidity so I'm just not sure what's happening. I added the method I'm using to retrieve the information; the insurancePlan is empty ....
    – namket
    Mar 21, 2021 at 22:11
  • If I run the getPatient() function, I also get all data successfully, so not sure what the problem might be on your side... Just in case, I used: pragma solidity ^0.7.0; pragma abicoder v2; Mar 22, 2021 at 6:00
  • By the way, if you want to access patient's data through address, a better approach would be to remove field 'address patient' from struct Patient, and use a mapping like mapping(address => Patient) public patients. This way you access the patient directly without iterating through the array. Mar 22, 2021 at 6:03
  • I really appreciate you taking the time to respond, thank you. I just tried on Remix and it appears to all work as well. Maybe I am accessing the method incorrectly as you originally pointed out? I am calling it in my JS using Web3 like so: const getPatient = await presContract.methods.getPatient(address).call(); When I do this, I get everything back except the insurancePlan is empty. As far as I can tell this syntax is correct?
    – namket
    Mar 22, 2021 at 13:48


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.