1

I want to use fields in my map() function, however I get this TypeError: Cannot read property 'fields' of undefined and I don't understand why. Because when I console.log(this.props.schema.fields) I get data printed out in my console.

Question

Why does is this happening? Is it because inside of the return() there is another scope? Or what..?

export default class ObjectDisplay extends Component {
  render() {
    console.log(this.props.schema.fields)
    //const { parentDocumentId, value, open, schema } = this.props
    return (
      <table>
        <tbody>
          {this.props.schema.fields.map((schema, i) =>
            <ObjectKeyDisplay
              key={i}
              schema={schema}
              value={(this.props.value || {})[schema.name]}
              parentDocumentId={this.props.parentDocumentId}
            />
          )}
        </tbody>
      </table>
    )
  }
}

I am really thankful for all the help.

2

2 Answers 2

3

It is giving you error because when first time it renders it it not able to find the fields property. Try to put condition using ternary operator.

{
  this.props.schema.fields
       ?
        ... Map function 
       : <div></div>
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Hey and thanks for the answer. Can you explain why it’s not being able to render it?
@MartinNordström You could find out why your schema.fields property is not set when you initially render your component. I would recommend checking the component that renders the ObjectDisplay and seeing if your schema object is defined at that point.
0

I was overriding my first constant schema with another constant named schema. Sorry for the misunderstandment!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.