2016-11-06 27 views
1

我試圖從我的應用程序中的ticketmaster API呈現json時不斷收到此錯誤。 JSX。我在後臺的網址是拉取數據以顯示前10場音樂會的結果。它在Chrome的反應控制檯中顯示得非常好,當我嘗試將結果映射到div時,出現此錯誤消息。請幫幫我。App.jsx:43未捕獲(承諾)TypeError:this.state.concerts.map不是函數(...)

import React, { Component } from 'react'; 

class App extends Component { 
    constructor() { 
    super(); 
    this.state = { 
     concerts: [], 
    } 
    this.handleZip = this.handleZip.bind(this); 
} 
    handleZip(e) { 
    e.preventDefault(); 
    let zip = e.target.value; 
    if (zip) { 
     fetch(`api/services/ticketmaster/shows/${zip}`) 
     .then((response) => { 
     response.json().then(concerts => this.setState({ 
      concerts: concerts._embedded, 
     // name: Object.values(concerts._embedded.events[0].images[0]), 
     // image: concerts._embedded.events[0].images[0], 
     // url: concerts._embedded.events[0].url, 
     // info: concerts._embedded.events[0].info, 
     // date: concerts._embedded.events[0].classifications[0].name, 
     })); 
     }); 
    } 
    } 
render() { 
    return (
     <div className="search-container"> 
     <div className="zipcode-search-bar"> 
     <input onBlur={this.handleZip} type="text" placeholder="Enter  your zip code" /> 
     </div> 
     <div className="zipcode-search-btn"> 
      <button className="zip-btn" onBlur= {this.handleZip}>Search</button> 
     </div> 
     <div> 
     { 
      this.state.concerts.map((concert) => { 
      return (
       <div> 
       {concert} 
       </div> 
      ); 
      }) 
     } 
     </div> 
    </div> 
); 
} 
} 

export default App; 

而這正是從特瑪數據看起來像郵遞員:

{ 
    "_embedded": { 
    "events": [ 
     { 
    "name": "Big Gigantic", 
    "type": "event", 
    "id": "k7vGFfodZESLi", 
    "test": false, 
    "url": "http://ticketmaster.com/event/00005110C09F37EC", 
    "locale": "en-us", 
    "images": [ 
     { 
     "ratio": "16_9", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025- b49a-5e20544c52e6_153341_EVENT_DETAIL_PAGE_16_9.jpg", 
     "width": 205, 
     "height": 115, 
     "fallback": false 
     }, 
     { 
     "ratio": "16_9", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_TABLET_LANDSCAPE_LARGE_16_9.jpg", 
     "width": 2048, 
     "height": 1152, 
     "fallback": false 
     }, 
     { 
     "ratio": "16_9", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RETINA_LANDSCAPE_16_9.jpg", 
     "width": 1136, 
     "height": 639, 
     "fallback": false 
     }, 
     { 
     "ratio": "3_2", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_TABLET_LANDSCAPE_3_2.jpg", 
     "width": 1024, 
     "height": 683, 
     "fallback": false 
     }, 
     { 
     "ratio": "16_9", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_TABLET_LANDSCAPE_16_9.jpg", 
     "width": 1024, 
     "height": 576, 
     "fallback": false 
     }, 
     { 
     "ratio": "3_2", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RETINA_PORTRAIT_3_2.jpg", 
     "width": 640, 
     "height": 427, 
     "fallback": false 
     }, 
     { 
     "ratio": "16_9", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RETINA_PORTRAIT_16_9.jpg", 
     "width": 640, 
     "height": 360, 
     "fallback": false 
     }, 
     { 
     "ratio": "16_9", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RECOMENDATION_16_9.jpg", 
     "width": 100, 
     "height": 56, 
     "fallback": false 
     }, 
     { 
     "ratio": "3_2", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_ARTIST_PAGE_3_2.jpg", 
     "width": 305, 
     "height": 203, 
     "fallback": false 
     }, 
     { 
     "ratio": "4_3", 
     "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_CUSTOM.jpg", 
     "width": 305, 
     "height": 225, 
     "fallback": false 
     } 
    ], 
    "sales": { 
     "public": { 
     "startDateTime": "2016-09-09T14:00:00Z", 
     "startTBD": false, 
     "endDateTime": "2016-11-06T00:00:00Z" 
     }, 
     "presales": [ 
     { 
      "startDateTime": "2016-09-07T14:00:00Z", 
      "endDateTime": "2016-09-09T02:00:00Z", 
      "name": "Venue Presale" 
     } 
     ] 
    }, 
    "dates": { 
     "start": { 
     "localDate": "2016-11-05", 
     "localTime": "21:00:00", 
     "dateTime": "2016-11-06T01:00:00Z", 
     "dateTBD": false, 
     "dateTBA": false, 
     "timeTBA": false, 
     "noSpecificTime": false 
     }, 
     "timezone": "America/New_York", 
     "status": { 
     "code": "offsale" 
     } 
    }, 
    "classifications": [ 
     { 
     "primary": true, 
     "segment": { 
      "id": "KZFzniwnSyZfZ7v7nJ", 
      "name": "Music" 
     }, 
     "genre": { 
      "id": "KnvZfZ7vAvF", 
      "name": "Dance/Electronic" 
     }, 
     "subGenre": { 
      "id": "KZazBEonSMnZfZ7vAJ1", 
      "name": "Club Dance" 
     } 
     } 
    ], 
    "promoter": { 
     "id": "653", 
     "name": "LIVE NATION MUSIC", 
     "description": "LIVE NATION MUSIC/NTL/USA" 
    }, 
    "info": "Big Gigantic has always been dedicated to giving back to those in need through their #ABigGiganticDifference Initiative. On this Brighter Future tour, they will continue the #ABigGiganticDifference mission by donating a percentage of ticket sales from each show to a local nonprofit of charity in need in each city. In our city of Huntington, they will be supporting the mission to collect toys for children in need by donating to Toys of Hope. If you'd like more information on Toys of Hope or would like to look into further volunteer or donation options with them, please visit http://www.toysofhope.org/contact_us.html. Thank you for continuing to support their initiative and by purchasing a ticket to this show, you are truly making #ABigGiganticDifference in our city!", 
    "priceRanges": [ 
     { 
     "type": "standard", 
     "currency": "USD", 
     "min": 25, 
     "max": 66 
     } 
    ], 
    "_links": { 
     "self": { 
     "href": "/discovery/v2/events/k7vGFfodZESLi?locale=en-us" 
     }, 
     "attractions": [ 
     { 
      "href": "/discovery/v2/attractions/K8vZ917GRLf?locale=en-us" 
     }, 
     { 
      "href": "/discovery/v2/attractions/K8vZ9173V70?locale=en-us" 
     } 
     ], 
     "venues": [ 
     { 
      "href": "/discovery/v2/venues/KovZpZAEAEtA?locale=en-us" 
     } 
     ] 
    }, 
    "_embedded": { 
     "venues": [ 
     { 
      "name": "The Paramount", 
      "type": "venue", 
      "id": "KovZpZAEAEtA", 
      "test": false, 
      "url": "http://ticketmaster.com/venue/1280", 
      "locale": "en-us", 
      "postalCode": "11743", 
      "timezone": "America/New_York", 
      "city": { 
      "name": "Huntington" 
      }, 
      "state": { 
      "name": "New York", 
      "stateCode": "NY" 
      }, 
      "country": { 
      "name": "United States Of America", 
      "countryCode": "US" 
      }, 
      "address": { 
      "line1": "370 New York Ave" 
      }, 
      "location": { 
      "longitude": "-73.42591099", 
      "latitude": "40.86969685" 
      }, 
      "markets": [ 
      { 
       "id": "35" 
      }, 
      { 
       "id": "124" 
      } 
      ], 
      "dmas": [ 
      { 
       "id": 296 
      }, 
      { 
       "id": 345 
      } 
      ], 
      "social": { 
      "twitter": { 
       "handle": "@TheParamountNY" 
      } 
      }, 
      "boxOfficeInfo": { 
      "phoneNumberDetail": "For more information please visit paramountny.com VIP Seating and Packages: Please call 631-673-7300 or visit paramountny.com", 
      "acceptedPaymentDetail": "Cash, Visa, MasterCard, Discover, Diners Club, & American Express", 
      "willCallDetail": "Location : Main Lobby Opens : 1 hour prior to show or event" 
      }, 
      "parkingDetail": "For parking information please visit : paramountny.com", 
      "accessibleSeatingDetail": "This is an accessible venue.", 
      "generalInfo": { 
      "generalRule": "NO cameras NO recording NO outside food or beverages (including alcohol) Backpacks and large bags must must be checked. ALL bags are subject to search. Smoking is NOT permitted", 
      "childRule": "Everyone needs a ticket to enter" 
      }, 
      "_links": { 
      "self": { 
       "href": "/discovery/v2/venues/KovZpZAEAEtA?locale=en-us" 
      } 
      } 
     } 
     ], 
     "attractions": [ 
     { 
      "name": "Big Gigantic", 
      "type": "attraction", 
      "id": "K8vZ917GRLf", 
      "test": false, 
      "url": "http://ticketmaster.com/artist/1474639", 
      "locale": "en-us", 
      "images": [ 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_EVENT_DETAIL_PAGE_16_9.jpg", 
       "width": 205, 
       "height": 115, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_TABLET_LANDSCAPE_LARGE_16_9.jpg", 
       "width": 2048, 
       "height": 1152, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RETINA_LANDSCAPE_16_9.jpg", 
       "width": 1136, 
       "height": 639, 
       "fallback": false 
      }, 
      { 
       "ratio": "3_2", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_TABLET_LANDSCAPE_3_2.jpg", 
       "width": 1024, 
       "height": 683, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_TABLET_LANDSCAPE_16_9.jpg", 
       "width": 1024, 
       "height": 576, 
       "fallback": false 
      }, 
      { 
       "ratio": "3_2", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RETINA_PORTRAIT_3_2.jpg", 
       "width": 640, 
       "height": 427, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RETINA_PORTRAIT_16_9.jpg", 
       "width": 640, 
       "height": 360, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_RECOMENDATION_16_9.jpg", 
       "width": 100, 
       "height": 56, 
       "fallback": false 
      }, 
      { 
       "ratio": "3_2", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_ARTIST_PAGE_3_2.jpg", 
       "width": 305, 
       "height": 203, 
       "fallback": false 
      }, 
      { 
       "ratio": "4_3", 
       "url": "http://s1.ticketm.net/dam/a/2e6/6c23d561-98d7-4025-b49a-5e20544c52e6_153341_CUSTOM.jpg", 
       "width": 305, 
       "height": 225, 
       "fallback": false 
      } 
      ], 
      "classifications": [ 
      { 
       "primary": true, 
       "segment": { 
       "id": "KZFzniwnSyZfZ7v7nJ", 
       "name": "Music" 
       }, 
       "genre": { 
       "id": "KnvZfZ7vAvF", 
       "name": "Dance/Electronic" 
       }, 
       "subGenre": { 
       "id": "KZazBEonSMnZfZ7vAJ1", 
       "name": "Club Dance" 
       } 
      } 
      ], 
      "_links": { 
      "self": { 
       "href": "/discovery/v2/attractions/K8vZ917GRLf?locale=en-us" 
      } 
      } 
     }, 
     { 
      "name": "Illenium", 
      "type": "attraction", 
      "id": "K8vZ9173V70", 
      "test": false, 
      "url": "http://ticketmaster.com/artist/2035639", 
      "locale": "en-us", 
      "images": [ 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/9c1/12b833e2-a349-4e59-b02a-9cad62cab9c1_118101_RETINA_PORTRAIT_16_9.jpg", 
       "width": 640, 
       "height": 360, 
       "fallback": false 
      }, 
      { 
       "ratio": "3_2", 
       "url": "http://s1.ticketm.net/dam/a/9c1/12b833e2-a349-4e59-b02a-9cad62cab9c1_118101_ARTIST_PAGE_3_2.jpg", 
       "width": 305, 
       "height": 203, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/c/df8/81eadad8-4449-412e-a2b1-3d8bbb78edf8_106181_TABLET_LANDSCAPE_16_9.jpg", 
       "width": 1024, 
       "height": 576, 
       "fallback": true 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/9c1/12b833e2-a349-4e59-b02a-9cad62cab9c1_118101_EVENT_DETAIL_PAGE_16_9.jpg", 
       "width": 205, 
       "height": 115, 
       "fallback": false 
      }, 
      { 
       "ratio": "4_3", 
       "url": "http://s1.ticketm.net/dam/a/9c1/12b833e2-a349-4e59-b02a-9cad62cab9c1_118101_CUSTOM.jpg", 
       "width": 305, 
       "height": 225, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/c/df8/81eadad8-4449-412e-a2b1-3d8bbb78edf8_106181_TABLET_LANDSCAPE_LARGE_16_9.jpg", 
       "width": 2048, 
       "height": 1152, 
       "fallback": true 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/a/9c1/12b833e2-a349-4e59-b02a-9cad62cab9c1_118101_RECOMENDATION_16_9.jpg", 
       "width": 100, 
       "height": 56, 
       "fallback": false 
      }, 
      { 
       "ratio": "16_9", 
       "url": "http://s1.ticketm.net/dam/c/df8/81eadad8-4449-412e-a2b1-3d8bbb78edf8_106181_RETINA_LANDSCAPE_16_9.jpg", 
       "width": 1136, 
       "height": 639, 
       "fallback": true 
      }, 
      { 
       "ratio": "3_2", 
       "url": "http://s1.ticketm.net/dam/c/df8/81eadad8-4449-412e-a2b1-3d8bbb78edf8_106181_TABLET_LANDSCAPE_3_2.jpg", 
       "width": 1024, 
       "height": 683, 
       "fallback": true 
      }, 
      { 
       "ratio": "3_2", 
       "url": "http://s1.ticketm.net/dam/a/9c1/12b833e2-a349-4e59-b02a-9cad62cab9c1_118101_RETINA_PORTRAIT_3_2.jpg", 
       "width": 640, 
       "height": 427, 
       "fallback": false 
      } 
      ], 
      "classifications": [ 
      { 
       "primary": true, 
       "segment": { 
       "id": "KZFzniwnSyZfZ7v7nJ", 
       "name": "Music" 
       }, 
       "genre": { 
       "id": "KnvZfZ7vAvF", 
       "name": "Dance/Electronic" 
       }, 
       "subGenre": { 
       "id": "KZazBEonSMnZfZ7vAJ1", 
       "name": "Club Dance" 
       } 
      } 
      ], 
      "_links": { 
      "self": { 
       "href": "/discovery/v2/attractions/K8vZ9173V70?locale=en-us" 
      } 
      } 
     } 
     ] 
    } 
    }, 
+0

是'concerts._embedded內events陣列「真的是一個數組? – topheman

+0

它是一個包含對象數組的對象。我將添加郵遞員數據 – Joemint1418

+2

如果您想映射某些內容,它必須是一個數組,因此當您執行setState({音樂會})時,請提供一個數組。 – topheman

回答

0

您將需要使用內events陣列,而不是外_embedded對象。您可以通過以下兩種方式之一來完成此操作。

設置狀態來保持內events陣列

response.json().then(concerts => this.setState({ 
      concerts: concerts._embedded.events, 
     // name: Object.values(concerts._embedded.events[0].images[0]), 
     // image: concerts._embedded.events[0].images[0], 
     // url: concerts._embedded.events[0].url, 
     // info: concerts._embedded.events[0].info, 
     // date: concerts._embedded.events[0].classifications[0].name, 
     })); 

訪問內時的使用狀態

this.state.concerts.events.map((concert) => { 
    return (
    <div> 
     {concert} 
    </div> 
); 
}) 
+0

非常感謝。這幫了很多。有用! – Joemint1418

+1

很高興爲您提供幫助。你能否將答案標記爲已接受,並且如果你感到慷慨的話,可以贊成它:-) – biofractal

相關問題