Search code examples
ethereumsolidity

Adding values to fixed array of strings only appends to the first element of the array


I am trying to create a function that returns an array of strings. Sadly instead of returning an array that has N number of elements of type string, it just appends all my string values to the first and only element in the array.

pragma solidity ^0.8.7;

contract Store is Ownable {
    struct Product {
        string name;
        uint32 quantity;
        bool exists;
    }

    uint64 private productId;

    mapping(uint => Product) private products;

    constructor() {
        productId = 1;
    }

    function addProduct(string calldata name, uint32 quantity) public onlyOwner
        require(quantity > 0, "Quantity cannot be negative integer");

        productId = productId + 1;

        products[productId] = Product(name, quantity, true);
    }

    function listProducts() external view returns (string[] memory){
        string[] memory productsInfo = new string[](productId);

        if(productId < 1){
            return productsInfo;
        }

        for(uint i = 0; i < productId; i++){
            string memory info = products[i + 1].name;

            productsInfo[i] = info;
        }

        return productsInfo;
    }
}

If I add products a, b, c. The json result from colling listProducts() looks like this:

{
    "0": "string[]: a,b,c"
}

Solution

  • the contract is working. Solidity display the elements in array with commas between them. Array return start with "0" string[].

    Hope it will help

    // SPDX-License-Identifier: GPL-3.0
    
    pragma solidity ^0.8.7;
    
    contract Store{
        struct Product {
            string name;
            uint32 quantity;
            bool exists;
        }
         address private Owner; 
         uint64 private productId;
         mapping(uint => Product) private products;
    
         constructor() {
            productId = 1;
            Owner = msg.sender;
         }
    
         modifier onlyOwner{
            require(msg.sender == Owner, "Fail! only admin can access this 
            function!");
             _;
          }
    
        function addProduct(string calldata _name, uint32 _quantity) public 
        onlyOwner{
            Product memory _strucObj;
            _strucObj.name = _name;
            _strucObj.quantity = _quantity;
            products[productId -1] = _strucObj;
            productId++;
        }
    
        function listProducts() external view returns (string[] memory){
            string[] memory productsInfo = new string[](productId);
    
            if(productId < 1){
                return productsInfo;
            }
    
            //dash symbol used for concatenation
            string memory dash = "-";
    
            for(uint i = 0; i < productId; i++){
                string memory info = products[i].name;
    
                productsInfo[i] = info;
            }
    
            return productsInfo;
        }
    
       function CheckProductsByIndex(uint index) public view returns(string memory name)
        {
             string[] memory productsInfo = new string[](productId);
             for(uint i = 0; i < productId; i++)
             {
                 string memory _toArray = products[i].name;
                 productsInfo[i] = _toArray;
             }
            return productsInfo[index];
        }
    }