Solidity 函数 returns 空数组到 web3.js

Solidity function returns empty array to web3.js

Solidity 函数 returns 一个字符串数组,在 Remix 和 truffle 控制台上执行时 returns 从 JS 调用时它是一个空数组

Solidity 代码

mapping(address => string[]) addressLink;
function getLinks(address a) public view returns (string[] memory)
  {
    return addressLink[a];
  }

JS代码

contract.methods.getLinks(accounts[0]).call().then(res => {
                 console.log(res)
             });

truffle 控制台结果和从 JS 调用时的预期结果

[ 'QmTiMLN8X4NE4ho5mqJ9t4bJ17JxfMHAFcg3z66f8vdUh1' ]

浏览器控制台上的结果(实际结果)

[""]
0: ""
length: 1

您可能在 javascript 代码中设置了错误的合约地址:

 const instance = new web3.eth.Contract(
  SimpleStorageContract.abi,
  deployedNetwork && deployedNetwork.address,
 );

第二个参数为合约地址,见:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract

希望对您有所帮助。

web3.js 库中存在错误。已在最新的测试版中关闭