0

I have a mapping of a struct, which has a mapping to another struct:

uint256 public questionIdCounter;
mapping(uint256 => Question) public questions;

    struct Question {
        uint256 answerCounter;
        mapping(uint256 => Answer) answers;
    }
    struct Answer {
        address answerer;
    }

And I'm looking to add a new Question, which can have Answers added to it.

function askQuestion() public {
        questions[questionIdCounter] = Question();
        questionIdCounter = questionIdCounter + 1;
    }

And then people can add answers

function answerQuestion(uint256 questionId) public {
        // Add new answer
        questions[questionId].answers[questions[questionId].answerCounter] = Answer({answerer: msg.sender});
        // update answer counter
        questions[questionId].answerCounter = questions[questionId].answerCounter + 1;
}

However, when I look at kbase_questions.questions(0) I only get to see the answerCounter and not the answer mapping.

Why is my empty mapping not being initialized?

I've looked at How to initialized struct with member variable type mapping and How to initialized struct with member variable type mapping, which seem to be the closest to what I'm looking to do, but they are declaring their struct outside the function, which I can't do because I have many structs.

4
  • 2
    This may be a debugger problem. Try to deploy a contract and see its behavior on a real network. – Mad Jackal Jan 29 at 14:52
  • 1
    What version of compiler are you using? I think you'll get an error in askQuestion() function in 0.7.0, because of the struct constructor Question(). You might have to redesign the structs so that they don't contain a mapping inside. – hrkrshnn Jan 30 at 14:19
  • Relevant: docs.soliditylang.org/en/v0.8.1/… – hrkrshnn Jan 30 at 14:21
  • Ahhhhhh. I'll have to check the version. This might be what's going on, these "silent skippers" – Patrick Collins Jan 31 at 20:28
1
  1. After v.0.7.0 and forward, you can't have nested mappings like this anyway.

  2. The debugger I was using wasn't showing me the mapping.

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.