I want to require two variables for the execution of my function, firstly a string shall not be undefined and then a mapping (address => uint) that shall be undefined. In the latter case, I have user numbers from 0...n, so 0 would be the first user. Is there an easy way to do this, or do I have to do complicated workarounds with eg. keccak hashes?

struct userData {
    string name;
    string role;   
    mapping (address => uint) accountToId;    

mapping (address => userData) addressToUserData; 

function register(string _name, string _role) public {
    require(_name !== false); // to avoid empty names
    require(addressToUserData[msg.sender] == false); // make sure mapping is undefined
    addressToUserData[msg.sender] = userData(_name, _role);

[I hope by now there is better solution than this]

  • Could you show some code? It is unclear what do you want so far. Apr 8, 2019 at 13:56
  • It's a pretty general question, but I added some code.
    – Marcellvs
    Apr 8, 2019 at 14:36

1 Answer 1

function register(string memory _name, string memory _role) public {
  require(bytes (_name).length > 0); // to avoid empty names
  require(bytes (addressToUserData[msg.sender].name).length == 0); // make sure mapping is undefined
  addressToUserData[msg.sender] = userData(_name, _role);
  • Thank you for you hint with the bytes typecasting. However, it won't work if the mapping maps to an integer, which is the case in my code. For e.g. require(bytes (datasets[_dataId].accountToId[_account]).length == 0) There is an error: TypeError: Explicit type conversion not allowed from "uint256" to "bytes memory"
    – Marcellvs
    Apr 10, 2019 at 17:31
  • Could you show your code? Apr 10, 2019 at 19:39
  • Sure, thank you: link --> function addUserToLoan
    – Marcellvs
    Apr 10, 2019 at 22:26

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.