This is my data structure for storing a list of questions which will be different for every address

struct Question{
    uint16 id;
    string question_string;
    string answer;

and this is stored as an array being the value of the mapping while address of the user being the key

mapping(address=>Question[]) public user_questions;

Now I want to update a particular question's answer for a user. What can I do for the same?

I tried using the following function:

function addAnswer(address _user, uint16 _id, string memory _answer) public {
    Question[] storage ques = user_questions[_user];
    ques[_id].answer = _answer;

but I get an error saying

[vm] from: 0x5B3...eddC4 to: AnonAMA.addAnswer(address,uint16,string) 0xdda...5482d value: 0 weight data: 0x0ed...00000 logs: 0 hash: 0xf7c...fdf84

transact to AnonAMA.addAnswer errored: VM error: revert.

revert The transaction has been reverted to the initial state.

Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

  • 3
    Before calling addAnswer() method did you add the question? Dec 18, 2022 at 12:41

2 Answers 2


It looks like you should add Question struct into the mapping array first, then you can update the _answer field.

I am not sure the Question was added before into the array mapping, but if it was not the code would look something like this:

function addQuestion(address _user, Question storage question) public returns (uint16) {

    // Returns Question array index (_id)
    return user_questions[_user].length - 1;

Once you have called this, the function that you have written addAnswer() should work - assuming _id is valid


Question[] is an array, not a mapping. You must push a Question into this array before you can set values on it. If you must dynamically set values without pushing into the array first, you must use a nested mapping.

mapping(address => mapping(uint16 => Question)) public user_questions;

Where the uint16 is your Question id. This eliminates the need for the array and allows for you to update values without having to initialize their array index.

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.