1

I have a function where I pass a id. With that id the contract find in a mapping a struct. My idea is to check if exists that struct with the given id with a if clause.

I try with 0, null, "".

Is that possible? Thx

  • 2
    Add a bool valid field to your struct, set it to true whenever you add a struct to the mapping, and use it in order to check whether or not the struct is valid (i.e., whether or not it "exists"). – goodvibration Jan 17 at 12:04
  • 1
    Like @goodvibration said, add a field to your struct, that you set when the record is created, then you can use that to check if it was initialised or not. Mappings that are not "initialised" will always return null / 0 / "" if data was not saved to said slot, so you need to check based on variables that actually change the state when saved. – Micky Socaci Jan 17 at 12:39
  • Thx for the support ;D – UnexpectedCharacter Jan 17 at 12:51
  • Have you seen this? ethereum.stackexchange.com/questions/13167/… – Rob Hitchens Jan 17 at 17:31
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.