質問編集履歴

2

変更

2021/01/30 13:17

投稿

suzukitoshinari
suzukitoshinari

スコア6

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- 該当箇所
9
+ 該当エラー箇所
10
10
 
11
11
  > src/Weather.js:59
12
12
 

1

内容の変更

2021/01/30 13:17

投稿

suzukitoshinari
suzukitoshinari

スコア6

test CHANGED
File without changes
test CHANGED
@@ -111,3 +111,163 @@
111
111
  }
112
112
 
113
113
  }
114
+
115
+
116
+
117
+
118
+
119
+ 追加箇所
120
+
121
+
122
+
123
+ > xhr.js:177 GET http://api.openweathermap.org/data/2.5/weather?q=city&appid=%22be690936e66de4f05bda2ccf9f534c2c%22 401 (Unauthorized)
124
+
125
+
126
+
127
+ > createError.js:16 Uncaught (in promise) Error: Request failed with status code 401
128
+
129
+ at createError (createError.js:16)
130
+
131
+ at settle (settle.js:17)
132
+
133
+ at XMLHttpRequest.handleLoad (xhr.js:62)
134
+
135
+
136
+
137
+ コード
138
+
139
+
140
+
141
+ ```
142
+
143
+ import { Button, Paper, TextField } from "@material-ui/core";
144
+
145
+ import React, { useState, useEffect } from "react";
146
+
147
+ import SendIcon from "@material-ui/icons/Send";
148
+
149
+ import axios from "axios";
150
+
151
+
152
+
153
+ const Weather = () => {
154
+
155
+ const [value, setValue] = useState("");
156
+
157
+ const [temp, settemp] = useState("");
158
+
159
+ const [typecity, settypecity] = useState("");
160
+
161
+ const [city, setcity] = useState("Delhi");
162
+
163
+ const [img, setimg] = useState("");
164
+
165
+ const [disc, setdisc] = useState("");
166
+
167
+
168
+
169
+ const citySelect = (e) => {
170
+
171
+ e.preventDefault();
172
+
173
+ setcity(typecity);
174
+
175
+ };
176
+
177
+ useEffect(() => {
178
+
179
+ axios.get(
180
+
181
+ `http://api.openweathermap.org/data/2.5/weather?q=city&appid="自身のAPI"`
182
+
183
+ ).then((res) => {
184
+
185
+ setValue(res.data);
186
+
187
+ settemp(res.data.main);
188
+
189
+ setimg(
190
+
191
+ `http://openweathermap.org/img/wn/res.data.weather[0].icon.png`
192
+
193
+ );
194
+
195
+ setdisc(res.data.weather[0].description);
196
+
197
+ });
198
+
199
+ }, [city]);
200
+
201
+
202
+
203
+ return (
204
+
205
+ <div>
206
+
207
+ <Paper className="paper">
208
+
209
+ <form onSubmit={citySelect} className="elementcenter">
210
+
211
+ <TextField
212
+
213
+ placeholder="type city here"
214
+
215
+ value={typecity}
216
+
217
+ onChange={(e) => settypecity(e.target.value)}
218
+
219
+ />
220
+
221
+ <Button type="submit">
222
+
223
+ <SendIcon />
224
+
225
+ </Button>
226
+
227
+ </form>
228
+
229
+ <h6 className="fontcss">{value.name}</h6>
230
+
231
+ <img src={img} alt="weather icon" className="imgcss" />
232
+
233
+ <h6 className="fontcss">{disc}</h6>
234
+
235
+ <div className="elementcenter">
236
+
237
+ <p>
238
+
239
+ Min <br />
240
+
241
+ {`${Math.floor(temp.temp_min - 273.15)}° C`}
242
+
243
+ </p>
244
+
245
+ <h6 className="fontcss">{`${Math.floor(temp.temp - 273.15)}° C`}</h6>
246
+
247
+ <p>
248
+
249
+ Min <br />
250
+
251
+ {`${Math.floor(temp.temp_max - 273.15)}° C`}
252
+
253
+ </p>
254
+
255
+ </div>
256
+
257
+ </Paper>
258
+
259
+ </div>
260
+
261
+ );
262
+
263
+ };
264
+
265
+
266
+
267
+ export default Weather;
268
+
269
+
270
+
271
+
272
+
273
+ ```