0

here is my code:

contract Notes {
    struct notes {
        string id;
        string content;
        address owner;
        bool isCreated;
        bool isDeleted;
        uint256 timestamp;
    }

    mapping(address => mapping(string =>notes)) private userNotes;

    function addNote(string memory _content, string memory _noteId) public {
        require(!userNotes[msg.sender][_noteId].isCreated,"a Note is already Created With This Id");
        userNotes[msg.sender][_noteId] =notes(_noteId, _content,msg.sender, true, false, now);
    }
}

So, above function addNote add data into struct notes with address indexing and string key. So, is there any way to return all struct data of msg.sender? everytime that function called by particular msg.sender, a new note is stored on struct right? i wanted to return all the notes ever created by msg.sender. not a single note.

1

There is no way to iterate over all keys inside a mapping, so there's no way to return all notes at the same time.

If you know all the note IDs of a user (because you store them in a database), you could iterate over these IDs on the client side, and call the function that @sp4c3 suggested to retrieve the notes one by one.

If you don't have this list of IDs, you could store an array instead of a mapping, as that can be iterated over without knowing the identifiers, as you know the size of the array.

mapping(address => notes[]) private userNotes;

Directly returning arrays of structs is not supported by Solidity, so you would still need to iterate over the array indices and retrieve all values one by one on the client side using web3.

You can try to use the experimental ABIEncoderV2, as shown in (Is there any way to return multiple structs (or the fields from them) in Solidity when one of the struct fields is of type `bytes`?). But note that this is an experimental feature, and is not guaranteed to work correctly.

0
pragma solidity 0.4.25;

contract Notes {
    struct notes {
        string id;
        string content;
        address owner;
        bool isCreated;
        bool isDeleted;
        uint256 timestamp;
                }
    mapping(address => mapping(string =>notes)) private userNotes;

    
  • Hello sp4c3, Thanks for your answer. you are not getting my point..everytime that function addNote called by particular msg.sender, a new note is stored on struct right? i wanted to return all the notes ever created by msg.sender. not a single note. – Salman Dev Feb 1 at 12:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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