Initiate dynamic string array

Viewed 390

I have the following contract

pragma solidity >=0.4.21 <0.7.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Sketch is ERC721 {
  string[] public sketchs;
  mapping(string => bool) _sketchExists;
  constructor() ERC721("Sketch", "SKETCH") public {
  }
  function mint(string memory _sketch) public {
    uint _id = sketchs.push(_sketch);
    _mint(msg.sender, _id);
    _sketchExists[_sketch] = true;
  }
}

But when I run truffle test I get the error message TypeError: Different number of components on the left hand side (1) than on the right hand side (0)" In relation to the line

uint _id = sketchs.push(_sketch)

How would I get around this? Do I have to initiate sketchs beforehand? What would that look like?

Any help would be appreciated

1 Answers

As solidity docs members->push(x): push(x) method returns nothing

push(x): Dynamic storage arrays and bytes (not string) have a member function called push(x) that you can use to append a given element at the end of the array. The function returns nothing.

You can change the code and get id like this:

sketchs.push(_sketch);
uint _id = sketchs.length - 1;
Related