Solidity - 无效的 BigNumber 字符串(参数 =“value”值 =“”代码 = INVALID_ARGUMENT 版本 = bignumber/5.4.2)

Xaa*_*rth 9 solidity remix

这里是坚固的新手。当我尝试读取 people 数组的值时。我收到错误:

对 SimpleStorage.people 的调用出错:对参数进行编码时出错:错误:无效的 BigNumber 字符串(参数 =“值”值 =“”代码=INVALID_ARGUMENT 版本=bignumber/5.4.2)

我的编译器版本是0.6.6。不确定出了什么问题?有什么建议么?

// SPD-License_Identifier: MIT

pragma solidity ^0.6.0;

contract SimpleStorage {
    uint256 favNum;
    
    struct People {
        uint256 favNum;
        string name;
    }
    
    People[] public people;
    
    function store(uint256 _favNum) public {
        favNum = _favNum;
    }
    
    function retrieve() public view returns(uint256) {
        return favNum;
    }
    
    function addPerson(string memory _name, uint256 _favNum) public {
        people.push(People(_favNum, _name));
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*jda 8

当您尝试调用people()函数(从 Remix IDE)而不传递任何值时,会发生错误。

由于People[] public people是公共属性,因此它会在编译期间自动生成getter 函数。但因为它是一个数组,所以 getter 函数需要一个uint256参数来指定要检索的数组的索引。

当您传递空字符串时,Remix 会尝试将其编码到BigNumber实例中,但这会失败。仅当您传递数组的(现有)索引时,它才能正常工作:

people() 调用


如果您想在一次调用中获取整个数组,则需要创建一个单独的 getter 函数:

function getAllPeople() public view returns (People[] memory) {
    return people;
}
Run Code Online (Sandbox Code Playgroud)

getAllPeople() 调用


Ale*_*tro 7

您必须单击部署按钮右侧的小箭头,然后将显示字段,以便您可以填写合约必须接收的数据。 在此输入图像描述