0

I am new here and I am suffering with the case below, I am trying to push data into an Array, but I can't make it work, the array continuous with zero length.

    contract StarNotary is ERC721 { 

    struct Star { 
        string name;
        string dec;
        string mag;
        string cent;
        string story;
    }

    using Library for Library.data;
    mapping(uint256 => Star) public tokenIdToStarInfo; 
    mapping(address => uint256[]) private _ownerToTokenId; 
    mapping(uint256 => uint256) private _starsForSale;
    mapping(bytes32 => Library.data) private _hashToTokenId;

    Star[] public stars;
    uint256[] tokensForSale;

    function createStar(string _name, string _dec, string _mag, string _cent, string _story) 
    public
    returns(uint256) 
    {
        require(!checkIfStarExist(_dec, _mag, _cent), "Star already registered!");

        Star memory newStar = Star(_name,_dec,_mag,_cent,_story);

        uint256 tokenId = stars.push(newStar) - 1;

        return tokenId;

    }
}

Do you know what can be happing? I look for it and find nothing.

Tks Ricardo

1

This code works fine. You'll have to show how you're testing this if you need more help. The following cleaned up code works in Remix. If I pass "a", "b", "c", "d", "e" to a call to createStar, then I can fetch element 0 of stars and see those values.

pragma solidity ^0.4.25;

contract StarNotary { 
    struct Star { 
        string name;
        string dec;
        string mag;
        string cent;
        string story;
    }

    Star[] public stars;

    function createStar(
        string _name,
        string _dec,
        string _mag,
        string _cent,
        string _story
    )
        public
        returns (uint256) 
    {
        Star memory newStar = Star(_name, _dec, _mag, _cent, _story);

        uint256 tokenId = stars.push(newStar) - 1;

        return tokenId;

    }
}
| improve this answer | |
  • Hello Smarx, I am using truffle to test, look at the lines bellow let tokenId = await this.contract.createStar.call('awesome star!', 'dec', 'mag', 'cent', 'story'); I always get token 0. – Ricardo Valim Nov 10 '18 at 19:11
  • 1
    State can't be modified in a call. Use sendTransaction instead. But note that you won't get the return value out of a transaction. Transactions don't have return values. You can log an event if you want to communicate the new tokenId out to the client. – user19510 Nov 10 '18 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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