Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS Failed to compile: 'Objects' is not defined no-undef

Can someone help me what is missing on my code? I'm trying to do this for a couple hours and error after error....

The objective is to fetch all objects coordinates into GoogleMaps Markers...

(Newbie at reactJS)

class maps extends Component{

    constructor(props){
        super(props)

        this.state = { Objects: [] }
    }

render(){

   {this.state.Objects.location.coordinates.map((item, i) => (
     <Marker position= {{ lat: Objects[i].location.coodinates[0], lng: Objects[i].location.coordinates[1] }}/>
    ))} 

    const MyMapComponent = withScriptjs(withGoogleMap((props) =>

    <GoogleMap
      defaultZoom={2}
      defaultCenter={{ lat: 40.6333333, lng: -8.65 }} 
    >

    </GoogleMap>
  ))

    return(
        <div>
        <MyMapComponent
        isMarkerShown
        googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyCnQmNcHpzoytzm02Zf-inD6xxTdSRJdLg&v=3.exp&libraries=geometry,drawing,places"
        loadingElement={<div style={{ height: `100%` }} />}
        containerElement={<div style={{ height: `400px` }} />}
        mapElement={<div style={{ height: `100%` }} />}
        />
        </div>
    )
}

componentDidMount(){

                axios.get('https://api.ost.pt/traffic_events/?key=VMSBvXtmOixXVuaHqeyAxDEhQkbnJpXZBYjDufEu').then( response => {

                    const { Objects } = response.data
                    console.log(Objects)
                    this.setState({Objects}) 

                })      
                .catch(error => {
                    console.log('Error fetching and parsing data', error); 
                });

            }

}

These are the objects.coordinates from api data that I need to fetch

Any hint will be appreciated. Thanks a lot!

like image 364
Cunha Avatar asked Feb 02 '26 02:02

Cunha


1 Answers

This is wrong:

{this.state.Objects.location.coordinates.map((item, i) => (
    <Marker position= {{ lat: Objects[i].location.coodinates[0], lng: Objects[i].location.coordinates[1] }}/>
))}

because you are mapping over this.state.Objects.location.coordinates, and you are using the index to access Objects[i].location.coodinates[0] and Objects[i].location.coordinates[1], and Objects isn't defined anywhere, but this.state.Objects is.

I'm guessing you meant something like this:

 {this.state.Objects.map((item, i) => (
        <Marker position= {{ lat: item.location.coodinates[0], lng: item.location.coordinates[1] }}/>
    ))}

Here's an example of how to list the Lat/Long for each Object: http://jsfiddle.net/g4hr5jnc/

You just replace it with the Google maps components

like image 173
Arber Sylejmani Avatar answered Feb 03 '26 16:02

Arber Sylejmani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!