Solidity函数将空数组返回到web3.js

时间:2019-03-25 07:27:31

标签: javascript solidity web3

Solidity函数在Remix和松露控制台上执行时返回一个字符串数组,而当从JS调用时返回一个空数组

实体代码

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)
             });
从JS调用

松露控制台结果和预期结果

[ 'QmTiMLN8X4NE4ho5mqJ9t4bJ17JxfMHAFcg3z66f8vdUh1' ]

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

[""]
0: ""
length: 1

2 个答案:

答案 0 :(得分:0)

您可能在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

希望有帮助。

答案 1 :(得分:0)

web3.js库中存在一个错误。在最新的Beta版本中已关闭

相关问题