Solidity中的 struct与mapping可访问性

solidity contract

contract Game {

  struct Land {  
    address user;  
    mapping(address=>uint) businessman;
  }

  Land[9] public lands; //九州

  function getBusinessman(uint landInx, address _user) public view returns(uint) {
    return lands[landInx].businessman[_user];
  }

}

Bulk access to lists/arrays/etc is painful in Solidity. You rarely see it in contracts. In your case, a possible solution is to provide a function to access one item, using its index, and to let the caller loops from 0 to id.

访问批量数据在Solidity中是很痛苦的

js script

let addr = "0x583031d1113ad414f02576bd6afabfb302140225";
let landInx=1;
let land = await contract.methods.lands(landInx).call();
land.user; // 可获取数据
land.businessman[addr]; // 获取不到数据

let res = await contract.methods.getBusinessman(landInx, addr).call();

console.log(res); //可获取数据

参考:

https://stackoverflow.com/a/37643972
https://github.com/ethereum/solidity/issues/40
https://medium.com/coinmonks/solidity-tutorial-returning-structs-from-public-functions-e78e48efb378

你可能感兴趣的:(Solidity中的 struct与mapping可访问性)