0

I am new to solidity and am confused on why I am getting this undeclared identifier error:

I have this modifier:

modifier isRegistered(uint ID_num){
    require (Array[ID_num].exists, "Object DNE");
    _;
}

Then I call it in this function but I am getting the undeclared identifier error.

function newAdoption(address addy1, address addy2) public isRegistered(ID_number){

}

Where am I going wrong here?

3 Answers 3

1

Your 'ID_number' in the modifier in the newAdoption function doesn't exists.

You shouldn't rely on an user input since any user can pass any value and it's therefore not trust worthy.

I don't know what you are trying to do, but you should try a pattern like this:

  struct MyUser {
    bool exists;
    address userAddress;
  }

  mapping (address => MyUser) public myUsers;

  modifier isRegistered(address userAddress){
    require (myUsers[userAddress].exists, "Object DNE");
    _;
  }

  function newAdoption(address addy1, address addy2) public isRegistered(msg.sender){

}

That way, your smart contract checks if the address contacting it is a user.

Edit: To make it more simple, you could also do that:


  mapping (address => bool) public myUsers;

  modifier isRegistered(address userAddress){
    require (myUsers[userAddress], "Object DNE");
    _;
  }

  function addUser(address _newUser) external onlyOwner() {
       myUsers[_newUser] = true;
}

  function newAdoption(address addy1, address addy2) public isRegistered(msg.sender){

}
2
  • So everytime a new user is created will the mapping automatically kick in and add that user into the hash map? @OlivierD
    – Ron
    Nov 7, 2022 at 14:55
  • Well you will have to create a addUser(address _newUser) function that is owner protected. Check my edited answer. Hope this helps Nov 7, 2022 at 15:35
0

The reason why it's showing that error is because ID_number doesn't exist in the function scope. Surely you don't have any state variable in this contract with the name ID_number and function arguments do not have that name either.
therefore ID_number must be a argument of that function (since arguments are already been included in the function scope when you use modifiers) or any other variable that belongs to that scope.

function newAdoption(address addy1, address addy2) public isRegistered(addy2){

}

function newAdoption(address addy1, address addy2) public isRegistered(any_variable_that_lies_in_this_scope){

}

Hope it helps.

0

function newAdoption(address addy1, address addy2) public isRegistered(ID_number)

in method signature you're pass ID_number in modifier but you're not passing ID_number param in newAdoption

you can just pass address addy1 or address addy2 in modifier

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.