Solidity

如何測試數組是否包含任何值?

  • July 14, 2022

這是我的智能合約

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;

contract SimpleStorage {
   uint256 public favoriteNumber;

   struct People {
       uint256 favoriteNumber;
       string name;
   }

   People[] public people;

   mapping(string => uint256) public nameToFavoriteNumber;

   function store(uint256 _favoriteNumber) public {
       favoriteNumber = _favoriteNumber;
   }

   function retrieve() public view returns (uint256) {
       return 
       favoriteNumber;
   }
   function addPerson(string memory _name, uint256 _favoriteNumber) public {
       people.push(People(_favoriteNumber, _name));
       nameToFavoriteNumber[_name] = _favoriteNumber;
   }
}

如果我嘗試像這樣通過安全帽訪問人員數組的第 0 個索引

 let peopleArray = await simpleStorage.people("0");
 console.log(`People ${peopleArray}`);

我正進入(狀態

Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="people(uint256)", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.6.4)
   at Logger.makeError (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\logger\src.ts\index.ts:261:28)
   at Logger.throwError (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\logger\src.ts\index.ts:273:20)
   at Interface.decodeFunctionResult (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\abi\src.ts\interface.ts:427:23)
   at Contract.<anonymous> (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\contracts\src.ts\index.ts:400:44)
   at step (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\contracts\lib\index.js:48:23)
   at Object.next (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\contracts\lib\index.js:29:53)
   at fulfilled (E:\Learning_Programming\Solidity\SimeplStorageHardHat\node_modules\@ethersproject\contracts\lib\index.js:20:58)
   at processTicksAndRejections (node:internal/process/task_queues:96:5) {
 reason: null,
 code: 'CALL_EXCEPTION',
 method: 'people(uint256)',
 data: '0x',
 errorArgs: null,
 errorName: null,
 errorSignature: null,
 address: '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707',
 args: [ '0' ],
 transaction: {
   data: '0x9e7a13ad0000000000000000000000000000000000000000000000000000000000000000',
   to: '0x5FC8d32690cc91D4c39d9d3abcBD16989F875707',
   from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
 }
}

但是如果我在添加一些值後訪問 people 數組

let transactionResponse = await simpleStorage.addPerson("john", "13531");
await transactionResponse.wait(1);

let peopleArray = await simpleStorage.people("0");
console.log(`People ${peopleArray}`);

我得到了值(因為它有一些價值嗎?)我的問題是如何檢查數組是否為空?通過安全帽?

我需要創建一個單獨公開此資訊的函式,例如:

function numOfPeople() public view returns (uint256) {
 return people.length;
}

呼叫此函式時,如果它返回任何大於 0 的值,則它不會為空。

您可以使用 array.length 方法通過 if-else 語句簡單地檢查:

if (peopleArray.length === 0) {
return console.log("Array is empty") 
} else { 
return console.log("Array Not Empty") 
} 

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