-1

this is my smart contract

// 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; } } 

if I tried to access people array's 0th index through hardhat like this

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

I am getting

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' } } 

but if I access the people array after adding some value

let transactionResponse = await simpleStorage.addPerson("john", "13531"); await transactionResponse.wait(1); let peopleArray = await simpleStorage.people("0"); console.log(`People ${peopleArray}`); 

I am getting the value(because it has some value right?) my question is how do I check whether an array is empty or not? through hardhat?

0

3 Answers 3

0

i need to create a function that exposes this information separately, e.g.:

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

When calling this function, if it returns anything above 0, it would not be empty.

-1

You can simply check that by an if-else statement with the array.length method:

if (peopleArray.length === 0) { return console.log("Array is empty") } else { return console.log("Array Not Empty") } 
1
  • It doesn't work. Commented Jul 16, 2022 at 22:22
-1

Your people array is public, you can call await simpleStorage.people.length; directly in test instead of creating the function numOfPeople() in the contract as mentioned in the previous answer

2
  • It doesn't work people is a getter function that expect a index parameter. Commented Jul 16, 2022 at 22:20
  • What do you mean it doesnt work ? I tested it before putting the comment. Commented Jul 17, 2022 at 22:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.