0

In the create function, I need the _title to be required and the _description to be optional. But when calling create without _title & with _description, it executes without any errors, but the _description gets assigned to the title in the struct.

Why isn't require working here? I'm using Remix Web.

Input

_title:
_description:Testing

search(0) output

0:string:Testing
1:string:

pragma solidity ^0.8.0;

contract Test {    

struct Object {
        uint256 id;
        string title;
        string description;        
    }

    mapping(uint256 => Object) objects;
    uint256 count;

    function create(string memory _title, string memory _description) public {
        require(bytes(_title).length > 0, "title must be set");
        
        objects[count] = Object(count, _title, _description);
        count++;
    }

    function search(uint256 _id) public view returns (string memory, string memory){
        return (objects[_id].title, objects[_id].description);
    }
}

1 Answer 1

2

If you're using Remix IDE, you should pass into _title parameter the following value: "" (empty quotes) for indicating it that specific parameter doesn't have any values. If you'll try in this way, you should see the error in require() statement.

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.