Solidity

TypeError:在 uint256 中進行參數相關查找後,成員“所有者”未找到或不可見

  • March 23, 2019

我對 Solidity 程式非常陌生,我在執行一個 Solidity 智能合約時遇到了問題。

pragma solidity >= 0.4 .22 < 0.6 .0;

contract DocumentManager {

mapping(uint => Document) documents;
uint public nbDocuments;
address public owner;

enum Status {
   UNKNOWN,
   OPEN,
   DONE,
   DENIED
}

struct Document {
   address owner;
   string document;
   string name;
   uint nbRequests;
   string privateKey;
   mapping(uint => Request) requests;
}

struct Request {
   address owner;
   Status status;
   string key;
}

constructor() public {
   owner = msg.sender;
}

function newDocument(string memory hash, string memory name) public {
   nbDocuments++;
   documents[nbDocuments].owner = msg.sender;
   documents[nbDocuments].document = hash;
   documents[nbDocuments].name = name;
   documents[nbDocuments].nbRequests = 0;
}

 function grantAccess(uint documentId, uint requestId, string memory 
  encryptedKey) public {
   uint256 document = documents[documentId];
   if (document.owner == msg.sender) {
       document.requests[requestId].status = Status.DONE;
       document.requests[requestId].key = encryptedKey;
    }
 }

function denyAccess(uint documentId, uint requestId) public {
   uint256 document = documents[documentId];
   if (document.owner == msg.sender) {
       document.requests[requestId].status = Status.DENIED;
   }
}

  function requestDocument(uint documentId, string memory publicKey) 
   public {
   uint256 document = documents[documentId];
   document.nbRequests++;
   uint256 request = document.requests[document.nbRequests];
   request.status = Status.OPEN;
   request.owner = msg.sender;
   request.key = publicKey;
 }

 function getLastRequestId(uint documentId) public returns(uint) {
   return documents[documentId].nbRequests;
  }

 function getOpenRequestPublicKey(uint documentId, uint requestId) 
  public returns(string memory) {
   uint256 request = documents[documentId].requests[requestId];
   if (request.status == Status.OPEN) {
       return request.key;
   }
   return "";
 }

function getRequestOwner(uint documentId, uint requestId) public 
 returns(address) {
   uint256 document = documents[documentId];
   if (document.owner == msg.sender) {
       return document.requests[requestId].owner;
   }
}

function getDocument(uint documentId) public returns(string memory 
 hash) {
   return documents[documentId].document;
}

function getDocumentName(uint documentId) public returns(string memory 
name) {
   return documents[documentId].name;
}

function getEncryptedKeyFromRequest(uint documentId, uint requestId) 
public returns(string memory) {
   uint256 request = documents[documentId].requests[requestId];
   if (request.status == Status.DONE) {
       return request.key;
   }
   return "";
}

function getDocumentHash(uint documentId) public returns(string 
memory) {
   return documents[documentId].document;
}

function getRequestStatus(uint documentId, uint requestId) public 
returns(Status) {
   return documents[documentId].requests[requestId].status;
}

}

當我遇到以下問題時

  on line number 39-40 
    browser/ballot.sol:39:9: TypeError: Type struct 
 DocumentManager.Document storage ref is not implicitly convertible 
  to expected type uint256.
   uint256 document = documents[documentId];
   ^--------------------------------------^

 browser/ballot.sol:40:12: TypeError: Member "owner" not found or not 
 visible after argument-dependent lookup in uint256.
   if(document.owner == msg.sender) {
      ^------------^

當我嘗試舊版本的solidity 時,它執行良好(pragma 4.22 版本)。

提前致謝

更改每次出現的情況:

uint256 document

對此:

Document document

每次出現這種情況:

uint256 request

對此:

Request request

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