0
    export default function Detail(props){

    return(
        <Modal  {...props}>
            <Modal.Header closeButton style={{ backgroundColor: '#6595FC', color:'white' }}>
                <Modal.Title style={{ wordBreak: 'break-all' }}>
                    {props.jobname} Details
                </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <Container>
                    <Row>
                        {props.detailData.detailData ? props.detailData.detailData[0].WORKFLOW_NAME : ''}
                        {props.detailData.detailData ? props.detailData.detailData[0].SQL_ID : ''} 
                    </Row>
                </Container>
            </Modal.Body>
            <Modal.Footer>
                <Button variant="primary" style={{ backgroundColor: '#6595FC' }}>Restart</Button>
            </Modal.Footer>
        </Modal>
    )
}

When there is data it works perfectly fine but when there is no data, it gives me the error

TypeError: Cannot read property 'WORKFLOW_NAME' of undefined

props.detailData.detailData ? props.detailData.detailData[0].WORKFLOW_NAME : ' ' isnt this suppose to work like a null & empty check?

1
  • You've established that detailData is defined but not that [0] is defined. If detailData is an empty array, then that'd cause this error, e.g. [][0].WORKFLOW_NAME reproduces the error. Please share a minimal reproducible example. Commented Jun 23, 2021 at 19:20

2 Answers 2

1

You're only checking if props.detailData.detailData itself is "truthy" (it exists). An empty array exists and passes this check, but it has no element at position 0 so the attempt to dereference that element will result in an error.

You can also add a length check:

(props.detailData.detailData && props.detailData.detailData.length > 0) ? props.detailData.detailData[0].WORKFLOW_NAME : ''

If you need to defend against other kinds of bad data you might also add an isArray() check, but that may be overkill.

Sign up to request clarification or add additional context in comments.

Comments

0

David's answer is perfect. But you also can do this:

props.detailData?.detailData[0]?.WORKFLOW_NAME || ''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.