Arrays
在反應中顯示結構數組內的結構數組
我正在嘗試創建不同類型的二維數組,其中一位使用者建議在 struct 方法中使用 struct ->不同類型的二維數組?
現在我遇到瞭如何在我的前端顯示它的問題。
所以這是我的2個結構:
struct Pvote { address voter; string comment; } struct Project{ uint id; int votes; address creator; string name; string decryption; string website; string imageLink; Pvote[] pvote; mapping(address => bool) voted; } Project[] public projects;
現在我使用 fill().map() 方法顯示結構的 Project 數組,如下所示:
const projectsList = await Promise.all( Array(parseInt(getProjects)) .fill() .map((element, index)=> { return Contract.methods.projects(index).call() }));
這完美地工作
現在我想顯示 pvote
$$ $$結構數組。因此,我創建了可靠的方法來幫助我將其從第一個結構中取出
function getPvoteStruct (uint index, uint index2 ) public view returns(address,string){ Project memory project = projects[index]; return(project.pvote[index2].voter, project.pvote[index2].comment); }
這在 Remix 中非常有效,但是當我嘗試從反應中呼叫此方法時:
const pVotesList = await Contract.methods.getPvoteStruct(0,0) console.log(pVotesList)
它給了我看起來像契約的 abi,我不知道為什麼:*是的,我確實確保在索引 0 處有一個項目,並且在投票數組索引 0 處有投票結構。
{ call: { [Function: bound _executeMethod] request: [Function: bound _executeMethod] }, send: { [Function: bound _executeMethod] request: [Function: bound _executeMethod] }, encodeABI: [Function: bound _encodeMethodABI], estimateGas: [Function: bound _executeMethod], arguments: [ 0, 0 ], _method: { constant: true, inputs: [ [Object], [Object] ], name: 'getPvoteStruct', outputs: [ [Object], [Object] ], payable: false, stateMutability: 'view', type: 'function', signature: '0x7b76417a' }, _parent: Contract { currentProvider: [Getter/Setter], _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} }, givenProvider: null, providers: { WebsocketProvider: [Function: WebsocketProvider], HttpProvider: [Function: HttpProvider], IpcProvider: [Function: IpcProvider] }, _provider: HttpProvider { host: 'https://rinkeby.infura.io/QqWsfQ60KqF4HCDRrP6I', timeout: 0, connected: true, headers: undefined }, setProvider: [Function], BatchRequest: [Function: bound Batch], extend: { [Function: ex] formatters: [Object], utils: [Object], Method: [Function: Method] }, clearSubscriptions: [Function], options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] }, defaultAccount: [Getter/Setter], defaultBlock: [Getter/Setter], methods: { projects: [Function: bound _createTxObject], '0x107046bd': [Function: bound _createTxObject], 'projects(uint256)': [Function: bound _createTxObject], getNvote: [Function: bound _createTxObject], '0x192e17cc': [Function: bound _createTxObject], 'getNvote(uint256)': [Function: bound _createTxObject], withdraw: [Function: bound _createTxObject], '0x3ccfd60b': [Function: bound _createTxObject], 'withdraw()': [Function: bound _createTxObject], remove: [Function: bound _createTxObject], '0x4cc82215': [Function: bound _createTxObject], 'remove(uint256)': [Function: bound _createTxObject], getPvoteStruct: [Function: bound _createTxObject], '0x7b76417a': [Function: bound _createTxObject], 'getPvoteStruct(uint256,uint256)': [Function: bound _createTxObject], getNumberOfvoters: [Function: bound _createTxObject], '0x870b9dc9': [Function: bound _createTxObject], 'getNumberOfvoters()': [Function: bound _createTxObject], getOwner: [Function: bound _createTxObject], '0x893d20e8': [Function: bound _createTxObject], 'getOwner()': [Function: bound _createTxObject], addNewProject: [Function: bound _createTxObject], '0xab508964': [Function: bound _createTxObject], 'addNewProject(string,string,string,string)': [Function: bound _createTxObject], getPvote: [Function: bound _createTxObject], '0xb7409fcf': [Function: bound _createTxObject], 'getPvote(uint256)': [Function: bound _createTxObject], temppvote: [Function: bound _createTxObject], '0xbb35e234': [Function: bound _createTxObject], 'temppvote(uint256)': [Function: bound _createTxObject], getCost: [Function: bound _createTxObject], '0xbd3e19d4': [Function: bound _createTxObject], 'getCost()': [Function: bound _createTxObject], upVote: [Function: bound _createTxObject], '0xc99d8eb8': [Function: bound _createTxObject], 'upVote(uint256,string)': [Function: bound _createTxObject], change_cost: [Function: bound _createTxObject], '0xc9bae9bc': [Function: bound _createTxObject], 'change_cost(uint256,uint256)': [Function: bound _createTxObject], getProjects: [Function: bound _createTxObject], '0xdcc60128': [Function: bound _createTxObject], 'getProjects()': [Function: bound _createTxObject], downVote: [Function: bound _createTxObject], '0xe3ea5ded': [Function: bound _createTxObject], 'downVote(uint256,string)': [Function: bound _createTxObject], editProject: [Function: bound _createTxObject], '0xe41c5f6f': [Function: bound _createTxObject], 'editProject(uint256,string,string,string,string)': [Function: bound _createTxObject] }, events: { allEvents: [Function: bound ] }, _address: '0x2b4Ad5532919A8fEB7bB0E555842d893C0b1F148', _jsonInterface: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ] }, _ethAccounts: Accounts { currentProvider: [Getter/Setter], _requestManager: RequestManager { provider: [Object], providers: [Object], subscriptions: {} }, givenProvider: null, providers: { WebsocketProvider: [Function: WebsocketProvider], HttpProvider: [Function: HttpProvider], IpcProvider: [Function: IpcProvider] }, _provider: HttpProvider { host: 'https://rinkeby.infura.io/MYAPI', timeout: 0, connected: true, headers: undefined }, setProvider: [Function], _ethereumCall: { getId: [Object], getGasPrice: [Object], getTransactionCount: [Object] }, wallet: Wallet { _accounts: [Circular], length: 0, defaultKeyName: 'web3js_wallet' } } }
我認為你應該使用
await Contract.methods.getPvoteStruct(0,0).call()
從結構返回數組也很痛苦。請參閱 web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id12
大聲笑似乎我忘記在 const pVotesList = await Contract.methods.getPvoteStruct(0,0) 末尾添加 .call() 感謝 grandfleet - > https://ethereum.stackexchange.com/users/44781/grandfleet應答器