如何在特定地址的映射中获得已创建结构的概述?

问题描述 投票:0回答:2

在我创建了一些属于特定地址的结构之后,我想要获得有关地址的相关参数的结构的概述。那么我该怎么做才能解决这个问题呢?

如果我在remix中运行我的代码,我只返回我的第一个存储的地址结构。但是我希望找回一个地址的所有存储结构。我知道我们不能迭代映射,但也许有可能为结构数组做一些索引计数器来解决它? - 那么是否也可以将数组的索引存储在变量中?

pragma solidity ^0.4.17;
contract Prescribe {

struct Prescription {
        address patients_address;
        string medicament;
        string dosage_form;
        uint amount;
        uint date;
        //uint index_counter;
 }

mapping (address => Prescription[]) public ownerOfPrescription;
address [] public patients;

function createPrescription(address patients_address, string 
medicament, string dosage_form, uint amount, uint date) public  
restricted {

ownerOfPrescription[patients_address].push(Prescription({
     patients_address: patients_address,
     medicament: medicament,
     dosage_form: dosage_form,
     amount: amount,
     date: date
}));
patients.push(patients_address);
}

function getOverview(address patient) public view restricted 
returns(string, string, uint, uint) {

for(uint i = 0; i < ownerOfPrescription[patient].length; i++) {
 if(ownerOfPrescription[patient][i].patients_address == patient) {
   return(ownerOfPrescription[patient][i].medicament, 
          ownerOfPrescription[patient][i].dosage_form, 
          ownerOfPrescription[patient][i].amount, 
          ownerOfPrescription[patient][i].date);
    }
  }

}

所以我希望在屏幕上的函数getOverview中获得一个地址的所有单独结构的返回值,但它只返回一个地址的第一个结构

struct mapping ethereum solidity smartcontracts
2个回答
0
投票

好吧,它只返回第一个,因为在声明之后

 if(ownerOfPrescription[patient][i].patients_address == patient)

返回true,您的代码正在执行return语句,该语句将使控件退出函数,并且不会执行进一步的语句。


0
投票

在研究之后我得出结论,仍然不可能将结构数组作为返回值。一个人只能访问阵列的各个元素吗? - 如果有关于此主题的任何更新,我将非常感谢提示。

© www.soinside.com 2019 - 2024. All rights reserved.