0

Inside my smart contract I have a struct type NotaryEntry and a function addNotaryEntry which creates an instance of the struct and add to a mapping, I do pass the right values to the function but the struct is created with zero values and ignores the values I parse to it even though I passed the right parameter types values to the function. How do I resolve this problem? Thanks

This is what function addNotaryEntry creates :

1: string: description
2: uint256: timestamp 0
3: string: hash
4: address: setBy 0x0000000000000000000000000000000000000000
5: bool: isSet false

Below is my code:

    uint filesCount;
    mapping(uint => NotaryEntry) public files;
    mapping(address => uint[]) public personFiles;

    struct NotaryEntry{
        uint id;
        string description;
        uint timestamp;
        string hash;
        address setBy;
        bool isSet;
    }

    function addNotaryEntry( string memory _hash, string memory _desc) public {

        uint _id = filesCount += 1;

        NotaryEntry memory notary;
        notary.id = _id;
        notary.description = _desc;
        notary.timestamp = now;
        notary.hash = _hash;
        notary.setBy = msg.sender;
        notary.isSet = true;
        personFiles[msg.sender].push(filesCount);
    }
`
2

Instead of creating a memory variable that will be discarded when the function is finished, you can use a storage pointer to write to storage.

Change this:

NotaryEntry memory notary;

To this:

NotaryEntry storage notary = files[filesCount];

Have a look over here for an explanation of why that works: https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089

This might be worth a look as well: Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

| improve this answer | |
  • This worked for me, thank you so much @Rob Hitchens – Richard Jul 23 '19 at 22:14
  • Cool. Thanks for accepting the answer if it worked. You should upvote all the answers that were helpful. It's not important that were100% right or the answer you accepted. Support the effort if they were useful. – Rob Hitchens Jul 23 '19 at 22:21
1

The issue is that you aren't writing the data to storage.

This line:

NotaryEntry memory notary;

Creates a new NotaryEntry and keeps it in memory. It will be disregarded at the end of the function, so any changes you make to it will be lost.

Instead, it looks like you should be writing to properties of the NotaryEntry mapping, ie:

files[_id].id = _id;
files[_id].description = _desc;
files[_id].timestamp = now;
///...etc
| improve this answer | |
0

It's tough to tell without seeing the rest of your code, but based on this function, it looks like you're making the new NotaryEntry in memory and never saving it to your persistent data structures. Try adding something like files.push(notary) at the bottom of addNotaryEntry. Then the below ought to behave:

function getNotaryEntry(uint int) public view {
  return files[id]
}

It looks like you're saving an empty object because when you define an array of structs in Solidity, empty array positions will look like an empty struct. You don't get an error or null value, you get a struct of the correct shape where each value is itself null-like.

Super weird behavior to me, took a while to get used to it.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.