0

I wanted to get array length by getter function, but it keeps reverts. But I have no idea why it's reverting. Here is my code.

struct ItemAddress {
    uint8 sendNatCd;
    uint8 rcvNatCd;
    uint16 ordID;
    string sendAddr;
    string rcvAddr;
}

ItemAddress[] public itemAddresses;

function createItemAddress(
    uint16 _ordID,
    uint8 _sendNatCd,
    uint8 _rcvNatCd,
    string calldata _sendAddr,
    string calldata _rcvAddr
)
    external
    returns
(
    uint256 itemAddressID
)
{
    ItemAddress memory _itemAddress = ItemAddress({
        ordID: _ordID,
        sendNatCd: _sendNatCd,
        rcvNatCd: _rcvNatCd,
        sendAddr: _sendAddr,
        rcvAddr: _rcvAddr
    });

    itemAddressID = itemAddresses.push(_itemAddress) - 1;

    return itemAddressID;
}

function getItemAddress(uint16 id)
    external
    view
    returns
(
    uint16 _ordID,
    uint8 _sendNatCd,
    uint8 _rcvNatCd,
    string memory _sendAddr,
    string memory _rcvAddr
)
{
    ItemAddress storage itemAddress = itemAddresses[id];

    _ordID = itemAddress.ordID;
    _sendNatCd = itemAddress.sendNatCd;
    _rcvNatCd = itemAddress.rcvNatCd;
    _sendAddr = itemAddress.sendAddr;
    _rcvAddr = itemAddress.rcvAddr;
}

function getItemLength()
    public
    view
    returns (uint256)
{
    return itemAddresses.length;
}

When I push data to itemAddresses array with createItemAddress(), I can get the data with getItemAddress() (ex. getItemAddress(0)). But when I call getItemLength(), it keeps reverting. So I tried itemAddresses.length at truffle console, and it returned 0. I'm confused that I could receive data from getItemAddress(), but why the itemAddresses array length is zero. What did I do wrong here?

1 Answer 1

0

I'm also confused about what is wrong with your tests. I simplified the code a bit (removed variables from the struct) and tried in Remix - everything seems to work:

pragma solidity ^0.5.0; 

contract A {
    struct ItemAddress {
        uint16 ordID;
    }

    ItemAddress[] public itemAddresses;

    function createItemAddress(
        uint16 _ordID
    )
        external
        returns
    (
        uint256 itemAddressID
    )
    {
        ItemAddress memory _itemAddress = ItemAddress({
            ordID: _ordID
        });

        itemAddressID = itemAddresses.push(_itemAddress) - 1;

        return itemAddressID;
    }

    function getItemAddress(uint16 id)
        external
        view
        returns
    (
        uint16 _ordID
    )
    {
        ItemAddress storage itemAddress = itemAddresses[id];

        _ordID = itemAddress.ordID;
    }

    function getItemLength()
        public
        view
        returns (uint256)
    {
        return itemAddresses.length;
    }
}

I also tried with your original code and that works just as fine.

1
  • I tried Remix now and it works well like you said. Hmm then my truffle environment should be something wrong. Weird.. Thanks for checking.
    – Jung Chun
    Commented Nov 12, 2019 at 8:56

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.