1

New to Firestore, React, and Redux. I have a document which contains a map called "questions" (see yellow highlight in Image 1 below). In this map there are question objects (underlined red 1, 2, 3 in Image 1). I was trying to dynamically deconstruct the questions map array, passing the data of each quesiton into a display component (to make it appear as a card on the Users Interface).

My code
Results in an error stating that "questions.map is not a function".

const QuestionList = ({questions}) => {      <------ Passed the "questions" map object into this react component.

    console.log(questions);                   <----- Output of console log is found in Image 2.  It appears to be correct.

    return(

        <div className='custom-question-list section'>

            { questions.map(question => {            <------ Error states that questions.map is not a function.
                console.log(question);               

                console.log(question.numberOfQuestions);

                return (

                    <div  key={question.id}>
                        <QuestionSummary question={question}/>   <----- Simply trying to take each individual question object and pass it into this QuestionSummary component (which helps display the content of that question object.

                    </div>
                
                )

                })
            }

        </div>

    )

}

IMAGE 1 enter image description here



IMAGE 2 enter image description here

FOUND A SOLUTION Thanks to Doug's response, I was able to find a solution. However, for future readers who might run into this problem, I have included the code that resolves the problem I was experiencing. You need to map over using the "Object.entries".

   return(

    <div className='custom-question-list section'>
        { Object.entries(questions).map(([key, question]) => ( 
            
            <div  key={key}>
                <QuestionSummary question={question}/>

            </div>

        ))

        }

    </div>

)
Andrew B.
  • 245
  • 3
  • 12

1 Answers1

1

I was trying to dynamically deconstruct the questions map array

There's no such thing as a "map array". The field's type is going to be either a map or an array. In your case, it's a map, not an array.

The error message is telling you that questions doesn't have a method called map. That's because it's not an array - it's a plain old JavaScript object whose key/value pairs match those in the map field. (Despite the fact that your map's keys appear to be numbers, they are actually just strings that look like numbers.)

If you want to iterate the key/value pairs in an object, there are plenty of ways to do that, as described here: How to iterate over a JavaScript object?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks so much for the clarification Doug! I was mistakenly assuming the .map function could work for both objects and arrays! You're definitely putting me back on the right path :) – Andrew B. Aug 03 '20 at 17:15