Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- this is my fetch:
- getDailyMenus(){
- return fetch("https://developers.zomato.com/api/v2.1/dailymenu?res_id=17937272", {
- headers: {
- Accept: "application/json",
- "User-Key": "api key from zomato"
- }
- })
- .then((data) => data.json())
- .then((res) => {this.setState({dataSource: (res)});console.log(res)})
- .catch((err) => {Alert.alert("Error!"); console.log(err);
- ;});
- }
- --------------------------------------------------------------------------------------------------------------------------------------
- and this is the response (log from console):
- Object {
- "daily_menus": Array [
- Object {
- "daily_menu": Object {
- "daily_menu_id": "20201786",
- "dishes": Array [
- Object {
- "dish": Object {
- "dish_id": "688202608",
- "name": "Polievka: Kulajda so strúhaným vajíčkom A:",
- "price": "1,90 €",
- },
- },
- Object {
- "dish": Object {
- "dish_id": "688202609",
- "name": "I. Bravčové Polpette so syrom so zemiakovou kašou a rukolovým olejom 150g/200g",
- "price": "",
- },
- },
- Object {
- "dish": Object {
- "dish_id": "688202610",
- "name": "A:7",
- "price": "5,49 €",
- },
- },
- Object {
- "dish": Object {
- "dish_id": "688202611",
- "name": "II. Kurací Cordon Bleu s domácimi americkými zemiakmi a tatárskou omáčkou 130g/200g",
- "price": "",
- },
- },
- Object {
- "dish": Object {
- "dish_id": "688202612",
- "name": "A:1,3,7,10,12",
- "price": "5,99 €",
- },
- },
- Object {
- "dish": Object {
- "dish_id": "688202613",
- "name": "III. Domáce Gnocchi s lososom, cherry rajčinami, baby špenátom a maslovou omáčkou 300g",
- "price": "",
- },
- },
- Object {
- "dish": Object {
- "dish_id": "688202614",
- "name": "A:4,7",
- "price": "6,49 €",
- },
- },
- ],
- "end_date": "2019-01-30 23:59:59",
- "name": "",
- "start_date": "2019-01-30 00:00:00",
- },
- },
- ],
- "status": "success",
- }
- --------------------------------------------------------------------------------------------------------------------------------------
- so I am trying to put it into flatlist:
- App.js
- <DailyMenusList itemList= {this.state.dataSource}/>
- DailyMenusList.js
- const DailyMenusList = ({ itemList }) => (
- <View style= {StylesJS.mainFlatList} >
- <FlatList
- data={itemList}
- renderItem={({item}) =>
- <View style= {{paddingBottom: 20}}>
- <Text style={StylesJS.restaurantHeader}>Mesto: {item.daily_menus.daily_menu.dishes}</Text>
- </View>
- }
- keyExtractor = {(item, index) => index.toString()}
- />
- </View>
- );
- --------------------------------------------------------------------------------------------------------------------------------------
- i thought that if the response is like that I would get to dishes with
- "item.daily_menus.daily_menu.dishes"
- but no. it is giving me an error:
- "Invariant Violation: Tried to get frame for out of range index NaN"
- --------------------------------------------------------------------------------------------------------------------------------------
- I dunno why, google said that I am giving out empty object, but in the console I get whole response.
- Then I thought maybe I am parsing it wrong.
- But I am not that far ahead in react-native
- --------------------------------------------------------------------------------------------------------------------------------------
- Now I tried to change it a little bit (added JSON.stringify(res)):
- getDailyMenus(){
- return fetch("https://developers.zomato.com/api/v2.1/dailymenu?res_id=17937272", {
- headers: {
- Accept: "application/json",
- "User-Key": "361c537f66afe2a8a9dc0c03cef9b91f"
- }
- })
- .then((data) => data.json())
- .then((res) => {/*this.setState({dataSource: (res)});*/console.log(JSON.stringify(res))})
- .catch((err) => {Alert.alert("Error!"); console.log(err);
- ;});
- }
- and the console output is:
- {"daily_menus":[{"daily_menu":{"daily_menu_id":"20201786","start_date":"2019-01-30 00:00:00","end_date":"2019-01-30 23:59:59","name":"","dishes":[{"dish":{"dish_id":"688202608","name":"Polievka: Kulajda so strúhaným vajíčkom A:","price":"1,90 €"}},{"dish":{"dish_id":"688202609","name":"I. Bravčové Polpette so syrom so zemiakovou kašou a rukolovým olejom 150g/200g","price":""}},{"dish":{"dish_id":"688202610","name":"A:7","price":"5,49 €"}},{"dish":{"dish_id":"688202611","name":"II. Kurací Cordon Bleu s domácimi americkými zemiakmi a tatárskou omáčkou 130g/200g","price":""}},{"dish":{"dish_id":"688202612","name":"A:1,3,7,10,12","price":"5,99 €"}},{"dish":{"dish_id":"688202613","name":"III. Domáce Gnocchi s lososom, cherry rajčinami, baby špenátom a maslovou omáčkou 300g","price":""}},{"dish":{"dish_id":"688202614","name":"A:4,7","price":"6,49 €"}}]}}],"status":"success"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement