Struct

為什麼這個功能不起作用,還有其他方法嗎?

  • June 3, 2018

對於函式 getAllcars,我想返回一個結構數組。然而,這似乎不能做到(還)。我嘗試了一種解決方法,方法是使用遞歸函式逐個返回每個客戶的詳細資訊;uint getCarsID 從零開始,並隨著每個循環遞增,直到它大於數組中的客戶數 (CarOwnerID),此時它將 getCarsID 的值重置為零,以便以後可以再次使用。

Remix 在編譯時接受此程式碼,並且在我執行它時不會引發任何問題,但它實際上並沒有返回任何內容。我有一半認為這無論如何都行不通,但我不知道為什麼。我錯過了什麼?如果無法做到這一點,是否有另一種方法可以一次返回結構數組中的所有資訊?

// <----------------------- END MEMBERS ----------------------->

struct Customer {

address addr;
string custname;
string color;
string make;
uint16 year;
string license;

}


// < -------------------------- CARS -------------------------- >



uint8 CarOwnerID;
Customer[] public carowners;
mapping(address => uint8) addressIndex;
mapping(string => uint8) licenseIndex;


function newCarOwner(address _address, string _custname, string _color, string _make, uint16 _year, string _license) onlyMember public {

carowners.push(Customer(_address, _custname, _color, _make, _year, _license));  

addressIndex[_address] = CarOwnerID;        //Testing to see if we can switch what
CarOwnerID = licenseIndex[_license];        //is on each side of the equation.

CarOwnerID ++;

}


function getCarByAdd(address _address) public view returns(address, string, string, string, uint16, string) {     // We must return each element in the struct 
   return (                                                                                                      // because we cannot return an entire struct (for now)
       carowners[addressIndex[_address]].addr,              // Notice - when returning elements of an array, use "," after each
       carowners[addressIndex[_address]].custname,          // element instead of ";", and put nothing after the last element.
       carowners[addressIndex[_address]].color,
       carowners[addressIndex[_address]].make,
       carowners[addressIndex[_address]].year,
       carowners[addressIndex[_address]].license
       );
}


function getCarByLic(string _license) public view returns(address, string, string, string, uint16, string) {
   return (
       carowners[licenseIndex[_license]].addr,             // While we can return whole arrays (ex. return carowners[];), and we can return specific indexed items
       carowners[licenseIndex[_license]].custname,         // such as uint returning array[3], array[4], etc., we cannot return a specific item if it is a struct.
       carowners[licenseIndex[_license]].color,            // AKA if array[3] is a struct, we cannot say return array[3], but must say "return array[3].param1, array{3].param2, etc.
       carowners[licenseIndex[_license]].make,             // See DOC1 in NOTES
       carowners[licenseIndex[_license]].year,
       carowners[licenseIndex[_license]].license
       );

}                                                           



uint8 public getCarsID;

function getAllCars() public returns(address, string, string, string, uint16, string) {
   return (
       carowners[getCarsID].addr,
       carowners[getCarsID].custname,
       carowners[getCarsID].color,
       carowners[getCarsID].make,
       carowners[getCarsID].year,
       carowners[getCarsID].license
       );

       getCarsID ++;

       if (getCarsID <= CarOwnerID) getAllCars;
       else (getCarsID = 0);
}


function countCars() public view returns (uint) {
   return carowners.length;
} 
function getAllCars() public returns(address, string, string, string, uint16, string) {
return (
   carowners[getCarsID].addr,
   carowners[getCarsID].custname,
   carowners[getCarsID].color,
   carowners[getCarsID].make,
   carowners[getCarsID].year,
   carowners[getCarsID].license
   );

   getCarsID ++;

   if (getCarsID <= CarOwnerID) getAllCars;
   else (getCarsID = 0);

}

此函式始終為您返回第零個索引車主。return 後的語句不可訪問。

您可以在前端或應用程序端處理此問題。這個想法是getCar一個一個地呼叫函式。由於您已經擁有countCars()函式,因此呼叫此函式並迭代以獲取每個車主,附加到前端的列表以執行其餘任務。

function getCar(uint8 index) returns (address, string,string,string,uint16,string){ return ( carowners[index].addr, carowners[index].custname, carowners[index].color, carowners[index].make, carowners[index].year, carowners[index].license ); }

getCar並在你的前端迭代這個函式。

for(int i=0;i<contractInstance.countCars();i++){
  contractInstance.getCar(i);
}

引用自:https://ethereum.stackexchange.com/questions/50291