0

I'm a JS dev fairly new to solidity. I couldn't find something to cover this when dealing with custom data. I want to create a function that I can use to push data into the contract. I'm trying to get nested data inside my contract but running into various errors each time I try something different, this is my most recent error.

"Member "push" not found or not visible after argument-dependent lookup in struct DustContr.Trait storage ref[] storage ref."

Here's my code. Any help or links to a good document would be appreciated

contract DustContract is ERC721Enumerable, ReentrancyGuard, Ownable {

    using Counters for Counters.Counter;
    using Strings for uint256;
    uint256 public constant maxFux = 100;
    bool public contractIsActive;
    Counters.Counter private _tokenIds;
    mapping (uint256 => string) private _tokenURIs;
    
    struct Funds {
        uint amount;
        address sender;
        uint time;
    }

    struct TraitData {
        uint256 id;
        string name;
        string svg;
    }

    struct Trait {
        TraitData[] common;
        TraitData[] uncommon;
        TraitData[] rare;
        TraitData[] epic;
        TraitData[] legendary;
    }
    
        struct Traits {
            Trait[] eyes;
            Trait[] body;
            Trait[] head;
        }
        
   Traits internal traits;
   Trait internal trait;
    TraitData internal traitData;
    
    function createTrait(
        uint256 _rarity,
        uint256 _traitType,
        uint256 _id,
        string calldata _name,
        string calldata _svg
        
    )
    external onlyOwner contractActive 
    {

        require(_traitType > 0);
        require(_rarity > 0);
        require(_id > 0);
        require(bytes(_name).length > 0);
        require(bytes(_svg).length > 0);

        trait.common.push(traits.eyes.push(TraitData(_id, _name, _svg)));

    }

This is the js version of what I'm trying to create

let Trait = {
        common:{
            eyes: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            body: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            head: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}]
        },
        uncommon:{
            eyes: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            body: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            head: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}]
        },
        rare:{
            eyes: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            body: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            head: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}]
        },
        epic:{
            eyes: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            body: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            head: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}]
        },
        legendary:{
            eyes: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            body: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}],
            head: [{"id":1,"name":"name","svg":"<svg></svg>"},{"id":2,"name":"name","svg":"<svg></svg>"}]
        }
}
0

traits.eyes.push(TraitData(_id, _name, _svg)) returns the new length of the array.

Since trait.common is of type TraitData, trying to push and integer fails and returns an error.

You might want to try (assuming you want to insert the same TraitData is trait.common).

uint idx = traits.eyes.push(TraitData(_id, _name, _svg)) - 1;
trait.common.push(traits.eyes[idx]);
3
  • Since solc 0.6 push(data) returns nothing.
    – Ismael
    Oct 8 '21 at 20:09
  • Thanks but still getting the same error, just on the new line
    – dust
    Oct 9 '21 at 12:34
  • I added more info around what I want the data structure to look like if that helps.
    – dust
    Oct 9 '21 at 13:32

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.