0
votes

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
2
if the contract is local? are you sure you already set the string before getting it from web3js? - Abdullah Aziz
Yes the contact is local. I have set the string through web3 and tried to retrieve from console at first which gives the expected result and when called from web3 it returns a empty array - Shivansh Nathani
can you show the complete code? I need to see how you are getting contract from web3. - Abdullah Aziz
cant paste the complete code here. Snippet of getting instance pastebin.com/mrjEmRmm - Shivansh Nathani

2 Answers

0
votes

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.

0
votes

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