1

I am new to react. My problem is that my variables keep saying that it is undefined. What I am trying to do is to display those variable but fail to destructure it. A filter function is executed and return a single tour. The data is successfully retrieved. By destructuring this, some variable contains an array can not be displayed. Does anyone know how to fix this?

TypeError: Cannot read property '0' of undefined

My data looks like this.

[
  { 
    "_id": "12345",
    "name": "I am first tour",
    "startLocation": {
       description: "USA",
       type: "point"
    },
    "startDates": [
      "2021-06-19T09:00:00.000Z",
      "2021-07-20T09:00:00.000Z",
    ],
    "imageUrl": [
      "https://something1.jpg",
      "https://something2.jpg",
      "https://something3.jpg",
    ],
  },
  //.....rest data
]
import React, { Component } from 'react';
import './Tour.css';
import { connect } from 'react-redux';

class Tour extends Component {
constructor(props) {
    super(props)
        this.findSingletour = this.findSingletour.bind(this);
    }

    findSingletour = (tourId) => {
        const notYetFilterTours = this.props.tours.tourState.data;
        let filtered = [];

        if (notYetFilterTours) {
            filtered = notYetFilterTours.find((tour) => {
                if (tour.id === tourId) {
                    return filtered;
                }
                return filtered;
            });
        }
        return filtered;
    };
        
    render() {
    const tourId = this.props.match.params._id;
    let SingleTour = this.findSingletour(tourId);
     
        const {
            name,
            startLocation,
            startDates,
            imageUrl
            } = SingleTour;

        return (
            <div>
                <span>{name}</span> // successfully rendered
                <span>{startLocation[0]}</span> // undefined
                <span>{startDates[0]}</span> // undefined
                <span>{imageUrl[0]}</span> // undefined
            </div>
        )
    }
}

const mapStateToProps = (state) => ({
    tours: state.tourContainer,
});

export default connect(
    mapStateToProps,
)(Tour);
2
  • Can you share what's behind // some code? Commented Jan 8, 2021 at 9:01
  • Console and check ''SingleTour'' there must be something wrong with that. Commented Jan 8, 2021 at 9:03

2 Answers 2

1

Need to do validation just in case:

class Tour extends Component {
    // some code
    render() {
     
        const {
            name,
            startLocation,
            startDates,
            imageUrl
            } = SingleTour;

        return (
            <div>
                <span>{name}</span> // successfully rendered
                <span>{startLocation && startLocation.length > 0 ? startLocation[0] : ''}</span> // undefined
                <span>{startDates && startDates.length > 0 ? startDates[0] : ''}</span> // undefined
                <span>{imageUrl && imageUrl.length > 0 ? imageUrl[0] : ''}</span> // undefined
            </div>
        )
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your answer is the most suitable in this case but I made a mistake. The startLocation variable is an object. How do I check the object with your answer? startLocation['description'] is undefined
if (startLocation && startLocation['description']) { }
1

You can provide default values, and it is generally a good idea to have sensible defaults in case data is not loaded and UI is rendered.

So something like this would prevent such errors:

const {
    name = '',
    startLocation = [],
    startDates = [],
    imageUrl = ''
} = SingleTour;

Now if your UI renders and tries to get 0 of startLocation, it won't fail. It will of course find nothing, and display nothing except the UI skeleton, but the app will not error out.

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.