2017-11-30 8 views
0

コードにないものを教えてもらえますか?私はReactJSコンパイルに失敗しました: 'オブジェクト'が定義されていませんno-undef

(reactJSで初心者)... Googleマップマーカーに

目的は、すべてのオブジェクトを取得することです....夫婦の時間とエラー後のエラーのためにこれを行うために座標をしようとしている

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

任意のヒントが理解されるであろう。どうもありがとう!

+0

を、 'Objects'は空ですアレイ。これがプロパティに 'location'プロパティを持つと仮定するのは公正ではありません。あなたは何を達成しようとしていますか? – brandNew

+1

あなたが反応した初心者なら、なぜ、「hello world」の代わりにこれを構築しようとしていますか? –

+0

コンポーネントが最初にiircをレンダリングするときにデータがまだAxiosからロードされているため、 'Objects'がアクセスしようとする前に' Objects'がデータでいっぱいであるかどうかをチェックするチェックを追加する必要があります。 ) 'は通常十分です。 – Andy

答えて

1

これは間違っている:あなたはthis.state.Objects.location.coordinatesの上にマッピングされているので、

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

、あなたはオブジェクトにアクセスするためにインデックスを使用している[i]が.location.coodinates [0]およびオブジェクト[i] .location.coordinates [1]およびオブジェクトは定義されていませんが、this.state.Objectsはです。

私はあなたがこのような何かを意味推測している:

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

ここでは、各オブジェクトの緯度/経度一覧表示する方法の例です:http://jsfiddle.net/g4hr5jnc/

あなただけのGoogleマップコンポーネントと交換

+0

上にマーカーを作成するために達成したい:それはまた、エラーを出力します:TypeError:item.location.coodinatesは定義されていません – Cunha

+0

あなたが "レンダリング"戻り、あなたのMyMapComponentをレンダーの外に置きます。ループは単なる問題の1つでした。 –

+0

私はちょうど緯度/経度を見ることができるようにJSFiddleで自分の回答を更新しました.Google Mapsコンポーネントでdivの内容を更新する必要があります。 –

0

私はコードがconstructor(props)を最初に実行し、次にrender()を実行し、次にcomponentDidMount()を実行することが問題だと思います。したがって、render()を実行すると、this.state.Objectsはまだ空の配列です。 componentDidMount()componentDidMount()

についてもっとcomponentWillMount()に変更
にしては、コンポーネントのライフサイクル反応するものは何でも
1.書き込み:
あなたは試したいことがあり最初は
http://busypeoples.github.io/post/react-component-lifecycle/

関連する問題