Advertisement
Johnathan3

Why can't they store normal json on their api?

Jan 30th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. this is my fetch:
  2. getDailyMenus(){
  3. return fetch("https://developers.zomato.com/api/v2.1/dailymenu?res_id=17937272", {
  4. headers: {
  5. Accept: "application/json",
  6. "User-Key": "api key from zomato"
  7. }
  8. })
  9. .then((data) => data.json())
  10. .then((res) => {this.setState({dataSource: (res)});console.log(res)})
  11. .catch((err) => {Alert.alert("Error!"); console.log(err);
  12. ;});
  13. }
  14. --------------------------------------------------------------------------------------------------------------------------------------
  15. and this is the response (log from console):
  16. Object {
  17. "daily_menus": Array [
  18. Object {
  19. "daily_menu": Object {
  20. "daily_menu_id": "20201786",
  21. "dishes": Array [
  22. Object {
  23. "dish": Object {
  24. "dish_id": "688202608",
  25. "name": "Polievka: Kulajda so strúhaným vajíčkom A:",
  26. "price": "1,90 €",
  27. },
  28. },
  29. Object {
  30. "dish": Object {
  31. "dish_id": "688202609",
  32. "name": "I. Bravčové Polpette so syrom so zemiakovou kašou a rukolovým olejom 150g/200g",
  33. "price": "",
  34. },
  35. },
  36. Object {
  37. "dish": Object {
  38. "dish_id": "688202610",
  39. "name": "A:7",
  40. "price": "5,49 €",
  41. },
  42. },
  43. Object {
  44. "dish": Object {
  45. "dish_id": "688202611",
  46. "name": "II. Kurací Cordon Bleu s domácimi americkými zemiakmi a tatárskou omáčkou 130g/200g",
  47. "price": "",
  48. },
  49. },
  50. Object {
  51. "dish": Object {
  52. "dish_id": "688202612",
  53. "name": "A:1,3,7,10,12",
  54. "price": "5,99 €",
  55. },
  56. },
  57. Object {
  58. "dish": Object {
  59. "dish_id": "688202613",
  60. "name": "III. Domáce Gnocchi s lososom, cherry rajčinami, baby špenátom a maslovou omáčkou 300g",
  61. "price": "",
  62. },
  63. },
  64. Object {
  65. "dish": Object {
  66. "dish_id": "688202614",
  67. "name": "A:4,7",
  68. "price": "6,49 €",
  69. },
  70. },
  71. ],
  72. "end_date": "2019-01-30 23:59:59",
  73. "name": "",
  74. "start_date": "2019-01-30 00:00:00",
  75. },
  76. },
  77. ],
  78. "status": "success",
  79. }
  80. --------------------------------------------------------------------------------------------------------------------------------------
  81. so I am trying to put it into flatlist:
  82.  
  83. App.js
  84. <DailyMenusList itemList= {this.state.dataSource}/>
  85.  
  86. DailyMenusList.js
  87. const DailyMenusList = ({ itemList }) => (
  88. <View style= {StylesJS.mainFlatList} >
  89. <FlatList
  90. data={itemList}
  91. renderItem={({item}) =>
  92. <View style= {{paddingBottom: 20}}>
  93. <Text style={StylesJS.restaurantHeader}>Mesto: {item.daily_menus.daily_menu.dishes}</Text>
  94. </View>
  95. }
  96. keyExtractor = {(item, index) => index.toString()}
  97. />
  98. </View>
  99. );
  100. --------------------------------------------------------------------------------------------------------------------------------------
  101. i thought that if the response is like that I would get to dishes with
  102. "item.daily_menus.daily_menu.dishes"
  103.  
  104. but no. it is giving me an error:
  105. "Invariant Violation: Tried to get frame for out of range index NaN"
  106. --------------------------------------------------------------------------------------------------------------------------------------
  107. I dunno why, google said that I am giving out empty object, but in the console I get whole response.
  108. Then I thought maybe I am parsing it wrong.
  109.  
  110. But I am not that far ahead in react-native
  111.  
  112. --------------------------------------------------------------------------------------------------------------------------------------
  113. Now I tried to change it a little bit (added JSON.stringify(res)):
  114. getDailyMenus(){
  115. return fetch("https://developers.zomato.com/api/v2.1/dailymenu?res_id=17937272", {
  116. headers: {
  117. Accept: "application/json",
  118. "User-Key": "361c537f66afe2a8a9dc0c03cef9b91f"
  119. }
  120. })
  121. .then((data) => data.json())
  122. .then((res) => {/*this.setState({dataSource: (res)});*/console.log(JSON.stringify(res))})
  123. .catch((err) => {Alert.alert("Error!"); console.log(err);
  124. ;});
  125. }
  126.  
  127. and the console output is:
  128. {"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