3

When I return a string array with web3 I don't get the original array as expected, but rather I get an array of the same size, with the index off by one.

So instead of ["One","Two","Three"] I get ["","One","Two"]

I am certain my contract is correct, because when I call the function on remix.ethereum.org I correctly get ["One","Two","Three"] as the return value.

Contract:

pragma experimental ABIEncoderV2;
pragma solidity ^0.5.0;

contract ProblemDemo {

    string[] private myStringArray;

    constructor () public {
        myStringArray.push("One");
        myStringArray.push("Two");
        myStringArray.push("Three");
    }

    function addToMyStringArray (string memory newEntry) public {
        myStringArray.push(newEntry);
    }

    function getMyStringArray () public view returns (string[] memory) {
        return myStringArray;
    }
}

Simple HTML displaying the issue:

<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
    <script src="https://github.com/ethereum/web3.js/blob/develop/dist/web3.min.js" type="text"></script>
    <h1>My Test</h1>
    <div id="output"></div>
    <button id="button">Click</button>
    <script>
        window.addEventListener('load', function() {
            if(typeof web3 !== undefined) web3js = new Web3(web3.currentProvider);

            document.getElementById("button").addEventListener("click", function() {
                getMyStringArray();
            }, function(error, result){}, false);
        });

        var contractInstance;
        var contractABI = [

    {
        "constant": false,
        "inputs": [
            {
                "name": "newEntry",
                "type": "string"
            }
        ],
        "name": "addToMyStringArray",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "getMyStringArray",
        "outputs": [
            {
                "name": "",
                "type": "string[]"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    }
];
        var contractAddress = "0x8fcb440a8ba9df8ea91493be3bd27ab562d04386";
<!-- Contract Address is on the Rinkeby Test Network -->
        contractInstance = web3.eth.contract(contractABI).at(contractAddress);


        function getMyStringArray() {
            contractInstance.getMyStringArray(function(error, result){
                document.getElementById("output").innerHTML = result;
            });
        }
    </script>
</body>
</html>
2
  • Writing an answer rather than a comment given my reputation. Did you ever find a solution for this ? I keep getting a wrong order of array, it's your same issue. Commented May 1, 2019 at 7:15
  • I did a workaround. I wrote a function that takes an uint as a parameter and then the function returns the string at the specified index. Commented May 2, 2019 at 13:20

2 Answers 2

0

string array is not available in Solidity because String is basically array of character . Nested dynamic arrays not implemented

1
  • It's not a Solidity problem. The experimental ABI encoder implements this functionality and it's working as intented. Calling the function in Remix IDE returns the expected values. What confuses me is that the query from web3js returns something different. Commented Nov 29, 2018 at 22:19
0

This problem is still an issue with the newest version of web3.min.js from cdn. I deployed a contract to test with goerli. Connecting via web3. The issue for me is with a struct which contains a string among other variables. Remix returns everything perfectly fine. But web3 getting return as memory returns everything EXCEPT the last string message value. Obviously this is unacceptable. Might file issue on github if nobody has any recommendations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.