質問編集履歴

4

Lhankor_Mhyさんにいただいた指摘で、内容を変更しました。

2021/03/27 03:50

投稿

ryota002
ryota002

スコア20

test CHANGED
File without changes
test CHANGED
@@ -297,3 +297,149 @@
297
297
  node.js v12
298
298
 
299
299
  firebase Authentication
300
+
301
+
302
+
303
+ ### 上手く行ったコード
304
+
305
+
306
+
307
+ > signup.js
308
+
309
+ ```js
310
+
311
+ "use strict";
312
+
313
+
314
+
315
+ import createUserData from "../createUserData.js";
316
+
317
+ import twitterOAuth from "../twitterOAuth.js";
318
+
319
+ import monitorLoginStatus from "../monitorLoginStatus.js";
320
+
321
+
322
+
323
+ const twitterSignup = () => {
324
+
325
+ const signUpButton = document.getElementById("signup");
326
+
327
+
328
+
329
+ signUpButton.addEventListener("click", () => {
330
+
331
+ twitterOAuth()
332
+
333
+ .then((userData) => {
334
+
335
+ console.log("signup関数の中", userData);
336
+
337
+ return createUserData(userData);
338
+
339
+ })
340
+
341
+ .then(() => {
342
+
343
+ console.log("monitorの前のログ");
344
+
345
+ monitorLoginStatus().then((loginStatus) => {
346
+
347
+ if (loginStatus) {
348
+
349
+ // location.replace("../../admin.html");
350
+
351
+ }
352
+
353
+ });
354
+
355
+ });
356
+
357
+ });
358
+
359
+ };
360
+
361
+
362
+
363
+ export default twitterSignup;
364
+
365
+ ```
366
+
367
+
368
+
369
+ > createUserData.js
370
+
371
+ ```js
372
+
373
+ "use strict";
374
+
375
+
376
+
377
+ const db = firebase.firestore();
378
+
379
+
380
+
381
+ const createUserData = (userData) => {
382
+
383
+ const uids = [];
384
+
385
+
386
+
387
+ return db
388
+
389
+ .collection("User")
390
+
391
+ .get()
392
+
393
+ .then((querySnapshot) => {
394
+
395
+ querySnapshot.forEach((doc) => {
396
+
397
+ uids.push(doc.data().uid);
398
+
399
+ });
400
+
401
+ })
402
+
403
+
404
+
405
+ .catch((error) => {
406
+
407
+ console.error("Error getting documents: ", error);
408
+
409
+ })
410
+
411
+ .then(() => {
412
+
413
+ const userRegistration = uids.includes(userData.uid);
414
+
415
+
416
+
417
+ if (!userRegistration) {
418
+
419
+ console.log("新規ユーザー。作る!");
420
+
421
+ console.log(userData);
422
+
423
+ db.collection("User")
424
+
425
+ .doc(userData.uid)
426
+
427
+ .set(userData)
428
+
429
+ .catch((error) => console.error(error));
430
+
431
+ } else {
432
+
433
+ console.log("既にユーザーある");
434
+
435
+ }
436
+
437
+ });
438
+
439
+ };
440
+
441
+
442
+
443
+ export default createUserData;
444
+
445
+ ```

3

更新

2021/03/27 03:50

投稿

ryota002
ryota002

スコア20

test CHANGED
File without changes
test CHANGED
@@ -112,7 +112,7 @@
112
112
 
113
113
  console.log("signup関数の中", userData);
114
114
 
115
- createUserData(userData);
115
+ await createUserData(userData);
116
116
 
117
117
  console.log("monitorの前のログ");
118
118
 

2

更新

2021/03/27 03:05

投稿

ryota002
ryota002

スコア20

test CHANGED
File without changes
test CHANGED
@@ -108,172 +108,172 @@
108
108
 
109
109
  signUpButton.addEventListener("click", async () => {
110
110
 
111
- await twitterOAuth().then((userData) => {
111
+ const userData = await twitterOAuth();
112
-
112
+
113
- console.log("signup関数の中", userData);
113
+ console.log("signup関数の中", userData);
114
-
114
+
115
- createUserData(userData);
115
+ createUserData(userData);
116
+
117
+ console.log("monitorの前のログ");
118
+
119
+ monitorLoginStatus().then((loginStatus) => {
120
+
121
+ if (loginStatus) {
122
+
123
+ // location.replace("../../admin.html");
124
+
125
+ }
116
126
 
117
127
  });
118
128
 
129
+ });
130
+
131
+ };
132
+
133
+
134
+
135
+ export default twitterSignup;
136
+
137
+
138
+
139
+ ```
140
+
141
+
142
+
143
+ > signup.js
144
+
145
+ ```js
146
+
147
+ "use strict";
148
+
149
+
150
+
151
+ import createUserData from "../createUserData.js";
152
+
153
+ import twitterOAuth from "../twitterOAuth.js";
154
+
155
+ import monitorLoginStatus from "../monitorLoginStatus.js";
156
+
157
+
158
+
159
+ const twitterSignup = () => {
160
+
161
+ const signUpButton = document.getElementById("signup");
162
+
163
+
164
+
165
+ signUpButton.addEventListener("click", () => {
166
+
167
+ twitterOAuth()
168
+
169
+ .then((userData) => {
170
+
171
+ console.log("signup関数の中", userData);
172
+
173
+ createUserData(userData);
174
+
175
+ })
176
+
177
+ .then(() => {
178
+
119
- console.log("monitorの前のログ");
179
+ console.log("monitorの前のログ");
120
-
180
+
121
- monitorLoginStatus().then((loginStatus) => {
181
+ monitorLoginStatus().then((loginStatus) => {
122
-
182
+
123
- if (loginStatus) {
183
+ if (loginStatus) {
124
-
184
+
125
- // location.replace("../../admin.html");
185
+ // location.replace("../../admin.html");
186
+
187
+ }
188
+
189
+ });
190
+
191
+ });
192
+
193
+ });
194
+
195
+ };
196
+
197
+
198
+
199
+ export default twitterSignup;
200
+
201
+ ```
202
+
203
+
204
+
205
+ > createUserData.js
206
+
207
+ ```js
208
+
209
+ "use strict";
210
+
211
+
212
+
213
+ const db = firebase.firestore();
214
+
215
+
216
+
217
+ const createUserData = (userData) => {
218
+
219
+ const uids = [];
220
+
221
+
222
+
223
+ db.collection("User")
224
+
225
+ .get()
226
+
227
+ .then((querySnapshot) => {
228
+
229
+ querySnapshot.forEach((doc) => {
230
+
231
+ uids.push(doc.data().uid);
232
+
233
+ });
234
+
235
+ })
236
+
237
+
238
+
239
+ .catch((error) => {
240
+
241
+ console.error("Error getting documents: ", error);
242
+
243
+ })
244
+
245
+ .then(() => {
246
+
247
+ const userRegistration = uids.includes(userData.uid);
248
+
249
+
250
+
251
+ if (!userRegistration) {
252
+
253
+ console.log("新規ユーザー。作る!");
254
+
255
+ console.log(userData);
256
+
257
+ db.collection("User")
258
+
259
+ .doc(userData.uid)
260
+
261
+ .set(userData)
262
+
263
+ .catch((error) => console.error(error));
264
+
265
+ } else {
266
+
267
+ console.log("既にユーザーある");
126
268
 
127
269
  }
128
270
 
129
271
  });
130
272
 
131
- });
132
-
133
273
  };
134
274
 
135
275
 
136
276
 
137
- export default twitterSignup;
138
-
139
- ```
140
-
141
-
142
-
143
- > signup.js
144
-
145
- ```js
146
-
147
- "use strict";
148
-
149
-
150
-
151
- import createUserData from "../createUserData.js";
152
-
153
- import twitterOAuth from "../twitterOAuth.js";
154
-
155
- import monitorLoginStatus from "../monitorLoginStatus.js";
156
-
157
-
158
-
159
- const twitterSignup = () => {
160
-
161
- const signUpButton = document.getElementById("signup");
162
-
163
-
164
-
165
- signUpButton.addEventListener("click", () => {
166
-
167
- twitterOAuth()
168
-
169
- .then((userData) => {
170
-
171
- console.log("signup関数の中", userData);
172
-
173
- createUserData(userData);
174
-
175
- })
176
-
177
- .then(() => {
178
-
179
- console.log("monitorの前のログ");
180
-
181
- monitorLoginStatus().then((loginStatus) => {
182
-
183
- if (loginStatus) {
184
-
185
- // location.replace("../../admin.html");
186
-
187
- }
188
-
189
- });
190
-
191
- });
192
-
193
- });
194
-
195
- };
196
-
197
-
198
-
199
- export default twitterSignup;
200
-
201
- ```
202
-
203
-
204
-
205
- > createUserData.js
206
-
207
- ```js
208
-
209
- "use strict";
210
-
211
-
212
-
213
- const db = firebase.firestore();
214
-
215
-
216
-
217
- const createUserData = (userData) => {
218
-
219
- const uids = [];
220
-
221
-
222
-
223
- db.collection("User")
224
-
225
- .get()
226
-
227
- .then((querySnapshot) => {
228
-
229
- querySnapshot.forEach((doc) => {
230
-
231
- uids.push(doc.data().uid);
232
-
233
- });
234
-
235
- })
236
-
237
-
238
-
239
- .catch((error) => {
240
-
241
- console.error("Error getting documents: ", error);
242
-
243
- })
244
-
245
- .then(() => {
246
-
247
- const userRegistration = uids.includes(userData.uid);
248
-
249
-
250
-
251
- if (!userRegistration) {
252
-
253
- console.log("新規ユーザー。作る!");
254
-
255
- console.log(userData);
256
-
257
- db.collection("User")
258
-
259
- .doc(userData.uid)
260
-
261
- .set(userData)
262
-
263
- .catch((error) => console.error(error));
264
-
265
- } else {
266
-
267
- console.log("既にユーザーある");
268
-
269
- }
270
-
271
- });
272
-
273
- };
274
-
275
-
276
-
277
277
  export default createUserData;
278
278
 
279
279
  ```

1

async/awaitで記載したものを追記しました。

2021/03/27 03:04

投稿

ryota002
ryota002

スコア20

test CHANGED
File without changes
test CHANGED
@@ -84,6 +84,62 @@
84
84
 
85
85
 
86
86
 
87
+ > signup.js(async/await)
88
+
89
+ ```js
90
+
91
+ "use strict";
92
+
93
+
94
+
95
+ import createUserData from "../createUserData.js";
96
+
97
+ import twitterOAuth from "../twitterOAuth.js";
98
+
99
+ import monitorLoginStatus from "../monitorLoginStatus.js";
100
+
101
+
102
+
103
+ const twitterSignup = () => {
104
+
105
+ const signUpButton = document.getElementById("signup");
106
+
107
+
108
+
109
+ signUpButton.addEventListener("click", async () => {
110
+
111
+ await twitterOAuth().then((userData) => {
112
+
113
+ console.log("signup関数の中", userData);
114
+
115
+ createUserData(userData);
116
+
117
+ });
118
+
119
+ console.log("monitorの前のログ");
120
+
121
+ monitorLoginStatus().then((loginStatus) => {
122
+
123
+ if (loginStatus) {
124
+
125
+ // location.replace("../../admin.html");
126
+
127
+ }
128
+
129
+ });
130
+
131
+ });
132
+
133
+ };
134
+
135
+
136
+
137
+ export default twitterSignup;
138
+
139
+ ```
140
+
141
+
142
+
87
143
  > signup.js
88
144
 
89
145
  ```js