質問編集履歴

3

UIの追加

2020/04/03 12:52

投稿

21212121
21212121

スコア61

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 現在FirebaseのでタータベースからデータをもらってAPIを叩いているのですが、このようなエラーが起こってしまいました。
1
+ ![イメージ説明](a8531ee1ff36efe826e0dbcde984ad58.png)現在FirebaseのでタータベースからデータをもらってAPIを叩いているのですが、このようなエラーが起こってしまいました。
2
2
 
3
3
  ```ここに言語を入力
4
4
 

2

コードの追加

2020/04/03 12:52

投稿

21212121
21212121

スコア61

test CHANGED
File without changes
test CHANGED
@@ -19,3 +19,269 @@
19
19
 
20
20
 
21
21
  ご教授いただけると幸いです。
22
+
23
+
24
+
25
+ Search.js
26
+
27
+ ```
28
+
29
+ import React,{useState,useEffect} from 'react';
30
+
31
+ import Card from '../UI/Card';
32
+
33
+ import './Search.css';
34
+
35
+
36
+
37
+ const Search = React.memo(props => {
38
+
39
+ const {onLoadIngredients} = props
40
+
41
+ const [enteredFilter,setEnteredFilter] = useState('');
42
+
43
+
44
+
45
+ useEffect(() => {
46
+
47
+
48
+
49
+ const query =
50
+
51
+ enteredFilter.length === 0
52
+
53
+ ? ''
54
+
55
+ : `?orderBy="title"&equalTo="${enteredFilter}"`;
56
+
57
+ fetch(`https://react-hooks-update-676a6.firebaseio.com/ingredients.json${query}`,
58
+
59
+ )
60
+
61
+ .then(response => {
62
+
63
+ console.log(response);
64
+
65
+ response.json()
66
+
67
+ })
68
+
69
+ .then(responseData => {
70
+
71
+ console.log(responseData)
72
+
73
+ const loadedIngredients = [];
74
+
75
+ for (const key in responseData) {
76
+
77
+ loadedIngredients.push({
78
+
79
+ id: key,
80
+
81
+ title: responseData[key].title,
82
+
83
+ amount: responseData[key].amount
84
+
85
+ });
86
+
87
+ }
88
+
89
+ onLoadIngredients(loadedIngredients);
90
+
91
+ });
92
+
93
+ }, [enteredFilter, onLoadIngredients]);
94
+
95
+
96
+
97
+
98
+
99
+ return (
100
+
101
+ <section className="search">
102
+
103
+ <Card>
104
+
105
+ <div className="search-input">
106
+
107
+ <label>Filter by Title</label>
108
+
109
+ <input
110
+
111
+ type="text"
112
+
113
+ value={enteredFilter}
114
+
115
+ onChange={event => setEnteredFilter(event.target.value)}
116
+
117
+ />
118
+
119
+ </div>
120
+
121
+ </Card>
122
+
123
+ </section>
124
+
125
+ );
126
+
127
+ });
128
+
129
+
130
+
131
+ export default Search;
132
+
133
+
134
+
135
+ ```
136
+
137
+ Ingredients.js
138
+
139
+ ``` 
140
+
141
+ import React,{useState,useEffect,useCallback} from 'react';
142
+
143
+ import IngredientList from './IngredientList'
144
+
145
+ import IngredientForm from './IngredientForm';
146
+
147
+ import Search from './Search';
148
+
149
+
150
+
151
+ function Ingredients() {
152
+
153
+
154
+
155
+ const [useringredients,setUserIngredients] = useState([]);
156
+
157
+ useEffect(() =>{
158
+
159
+ fetch('https://react-hooks-update-676a6.firebaseio.com/ingredients.json').then(
160
+
161
+ responese=>responese.json()
162
+
163
+ ).then(responeseData=>{
164
+
165
+ const loadedIngredients = [];
166
+
167
+ for(const key in responeseData){
168
+
169
+ loadedIngredients.push({
170
+
171
+ id:key,
172
+
173
+ title:responeseData[key].title,
174
+
175
+ amount:responeseData[key].amount
176
+
177
+ })
178
+
179
+ }
180
+
181
+ setUserIngredients(loadedIngredients);
182
+
183
+ })
184
+
185
+ },[]);
186
+
187
+
188
+
189
+ useEffect(() =>{
190
+
191
+ console.log('RENDRING INGREDIENTS',useringredients)
192
+
193
+ },[useringredients])
194
+
195
+
196
+
197
+ const filteredIngredientsHandler = useCallback(filteredIngredients => {
198
+
199
+ setUserIngredients(filteredIngredients);
200
+
201
+ }, []);
202
+
203
+
204
+
205
+ const addIngredientHandler = ingredient => {
206
+
207
+ fetch('https://react-hooks-update-676a6.firebaseio.com/ingredients.json', {
208
+
209
+ method: 'POST',
210
+
211
+ body: JSON.stringify(ingredient),
212
+
213
+ headers: { 'Content-Type': 'application/json' }
214
+
215
+ })
216
+
217
+ .then(response => {
218
+
219
+ return response.json();
220
+
221
+ })
222
+
223
+ .then(responseData => {
224
+
225
+ setUserIngredients(prevIngredients => [
226
+
227
+ ...prevIngredients,
228
+
229
+ { id: responseData.name, ...ingredient }
230
+
231
+ ]);
232
+
233
+ });
234
+
235
+ };
236
+
237
+
238
+
239
+ const removeIngredientHandler = ingredientId => {
240
+
241
+ setUserIngredients(prevIngredients =>
242
+
243
+ prevIngredients.filter(ingredient => ingredient.id !== ingredientId)
244
+
245
+ );
246
+
247
+ };
248
+
249
+
250
+
251
+
252
+
253
+ return (
254
+
255
+ <div className="App">
256
+
257
+ <IngredientForm onAddIngredient={addIngredientHandler}/>
258
+
259
+
260
+
261
+ <section>
262
+
263
+ <Search onLoadIngredients={filteredIngredientsHandler}/>
264
+
265
+  <IngredientList ingredients={useringredients} onRemoveItem={removeIngredientHandler}/>
266
+
267
+ </section>
268
+
269
+ </div>
270
+
271
+ );
272
+
273
+ }
274
+
275
+
276
+
277
+ export default Ingredients;
278
+
279
+
280
+
281
+ ```
282
+
283
+ こちらコードです。
284
+
285
+ 現在Udemyを元に学習しているので、中々質問がしづらくなってしまいました。
286
+
287
+ よろしくお願いします。

1

エラーメッセージ

2020/04/03 12:51

投稿

21212121
21212121

スコア61

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  ```ここに言語を入力
4
4
 
5
-
5
+ {
6
+
7
+ "error" : "Index not defined, add \".indexOn\": \"title\", for path \"/ingredients\", to the rules"
8
+
9
+ }
6
10
 
7
11
  ```
8
12