简体   繁体   中英

Solidity function returns empty array to web3.js

Solidity function returns a array of string while executed on Remix as well as truffle console while it returns a empty array when called from JS

Solidity code

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

JS code

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

truffle console result and expected results when called from JS

[ 'QmTiMLN8X4NE4ho5mqJ9t4bJ17JxfMHAFcg3z66f8vdUh1' ]

Result on browser console(Actual result)

[""]
0: ""
length: 1

You might set the wrong contract address in your javascript code:

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

The second parameter is contract address, see: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract

Hope it helps.

There was a bug in web3.js library. Which has been closed in the latest beta release

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM