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

问题描述 投票:0回答:2

Solidity函数在Remix和truffle控制台上执行时返回一个字符串数组,当从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)
             });

从JS调用时,松露控制台结果和预期结果

[ 'QmTiMLN8X4NE4ho5mqJ9t4bJ17JxfMHAFcg3z66f8vdUh1' ]

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

[""]
0: ""
length: 1
javascript solidity web3
2个回答
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

希望能帮助到你。


0
投票

web3.js库中有一个错误。哪个已在最新的测试版中关闭

© www.soinside.com 2019 - 2024. All rights reserved.