質問編集履歴

6

内容修正

2021/05/27 10:20

投稿

TMTN
TMTN

スコア53

test CHANGED
File without changes
test CHANGED
@@ -120,6 +120,8 @@
120
120
 
121
121
  分かる方いらっしゃいましたらお力添えをいただきたいです。
122
122
 
123
+
124
+
123
125
  お手数おかけしますが、よろしくおがいいたします。
124
126
 
125
127
 

5

追記3追加

2021/05/27 10:20

投稿

TMTN
TMTN

スコア53

test CHANGED
File without changes
test CHANGED
@@ -637,3 +637,89 @@
637
637
 
638
638
 
639
639
  [【Vue x RealtimeDatabase】Firestoreにてデータを保存しているユーザー情報を取得して名前とアンコンを表示したいです。](https://qiita.com/TMTN/questions/61264bafb8e7aea37e24)
640
+
641
+
642
+
643
+ #追記3
644
+
645
+
646
+
647
+
648
+
649
+
650
+
651
+ ```ここに言語を入力
652
+
653
+ this.userIds.forEach((id) => {
654
+
655
+ firebase
656
+
657
+ .firestore()
658
+
659
+ .collection("users")
660
+
661
+ .doc(id)
662
+
663
+ .get()
664
+
665
+ .then((snapshot) => {
666
+
667
+ this.userDatas.push(snapshot.data());
668
+
669
+ });
670
+
671
+ });
672
+
673
+ ```
674
+
675
+ 上記forEachがオブサーバー型になっており、機能していなかったため、下記の段階でjson型に変えてみました。
676
+
677
+ しかし、if文に入ったthis.userIdsをコンソールを確認しましたが、再びオブサーバー型になってしまいます。
678
+
679
+
680
+
681
+ message.userIdのデータ型がstringになるようにしてから、this.userIdsにpushしたいのですが、
682
+
683
+ stringにして渡すやり方をお伺いしたいです。
684
+
685
+
686
+
687
+
688
+
689
+ ```ここに言語を入力
690
+
691
+ childAdded(snap) {
692
+
693
+ const message = JSON.parse(JSON.stringify(snap.val()));
694
+
695
+ if (!this.userIds.includes(String(message.userid)))
696
+
697
+ this.userIds.push(String(message.userid));
698
+
699
+
700
+
701
+ console.log(this.userIds);
702
+
703
+ this.chat.push({
704
+
705
+ key: snap.key,
706
+
707
+ name: message.name,
708
+
709
+ image: message.image,
710
+
711
+ message: message.message,
712
+
713
+ userid: message.userid,
714
+
715
+ time: message.time,
716
+
717
+ });
718
+
719
+ this.scrollBottom();
720
+
721
+
722
+
723
+ },
724
+
725
+ ```

4

追記2追加

2021/05/26 16:37

投稿

TMTN
TMTN

スコア53

test CHANGED
File without changes
test CHANGED
@@ -625,3 +625,15 @@
625
625
 
626
626
 
627
627
  宜しくお願い致します。
628
+
629
+
630
+
631
+ #追記2
632
+
633
+
634
+
635
+ 長期間回答が得られない為、以下サイトにも掲載させて頂いております。
636
+
637
+
638
+
639
+ [【Vue x RealtimeDatabase】Firestoreにてデータを保存しているユーザー情報を取得して名前とアンコンを表示したいです。](https://qiita.com/TMTN/questions/61264bafb8e7aea37e24)

3

追記1追加しました。

2021/05/25 13:30

投稿

TMTN
TMTN

スコア53

test CHANGED
@@ -1 +1 @@
1
- 【Vue x RealtimeDatabase】Firestoreにてデータを保存しているユーザー情報を取得して表示したい
1
+ 【Vue x RealtimeDatabase】Firestoreにてデータを保存しているユーザー情報を取得して名前とアンコンを表示したいです。
test CHANGED
@@ -561,3 +561,67 @@
561
561
  </script>
562
562
 
563
563
  ```
564
+
565
+
566
+
567
+ #追記1
568
+
569
+
570
+
571
+ ```ここに言語を入力
572
+
573
+ const currentUser = firebase.auth().currentUser;
574
+
575
+ //現在ログインしているユーザーを取得
576
+
577
+ this.uid = currentUser.uid;
578
+
579
+
580
+
581
+ this.userIds.forEach((id) => {
582
+
583
+ firebase
584
+
585
+ .firestore()
586
+
587
+ .collection("users")
588
+
589
+ .doc(id)
590
+
591
+ .get()
592
+
593
+ .then((snapshot) => {
594
+
595
+ this.userDatas.push(snapshot.data());
596
+
597
+ });
598
+
599
+ });
600
+
601
+ console.log(this.userIds);
602
+
603
+ },
604
+
605
+ ```
606
+
607
+ ![イメージ説明](79a34bbc045513243e6ade46249980a2.png)
608
+
609
+
610
+
611
+ 上記、現状this.userIds.forEach((id)の部分でforEachが機能していない状況です。
612
+
613
+ this.userIdsの値は画像にあるようにobserverでデータは取得できていますが、配列でない為、
614
+
615
+ データがforEachにわたっていないように見受けられます。
616
+
617
+
618
+
619
+ ※ this.userDatasの値を確認しようとしてもコンソール自体機能しなかった為。
620
+
621
+
622
+
623
+ 引き続き分かる方いらっしゃいましたらお力添えを頂きたいです。
624
+
625
+
626
+
627
+ 宜しくお願い致します。

2

内容変更

2021/05/25 13:14

投稿

TMTN
TMTN

スコア53

test CHANGED
File without changes
test CHANGED
@@ -6,604 +6,558 @@
6
6
 
7
7
 
8
8
 
9
- ![画像](da1437315a265a18519d748ca8601b3a.png)
9
+ ![画像](4aaa1e4f08f0d03c4e045ce81a609f1f.png)
10
+
11
+
12
+
10
-
13
+ Firestoreを使用してユーザー情報を管理しているのですが、
11
-
12
-
14
+
13
- Firestoreを使用してユーザー情報を管理しているのですが、Realtime Databaseでそのユーザー情報を表示させたいと考えていますが、表示の仕方がわかりせん。。
15
+ Realtime Databaseでそのユーザー情報を表示させたいと考えていますが、行き詰ってる状況です・・
14
16
 
15
17
 
16
18
 
17
19
  ```ここに言語を入力
18
20
 
21
+ created() {
22
+
23
+
24
+
25
+   ~ 省略 ~
26
+
27
+
28
+
19
- const currentUser = firebase.auth().currentUser;
29
+ const currentUser = firebase.auth().currentUser;
30
+
31
+ //現在ログインしているユーザーを取得
20
32
 
21
33
  this.uid = currentUser.uid;
22
34
 
35
+
36
+
37
+ this.userIds.forEach((id) => {
38
+
23
- firebase
39
+ firebase
24
-
40
+
25
- .firestore()
41
+ .firestore()
26
-
42
+
27
- .collection("users")
43
+ .collection("users")
28
-
29
- .doc(currentUser.uid)
44
+
30
-
31
- .get();
32
-
33
- firebase
34
-
35
- .firestore()
36
-
37
- .collection("users")
38
-
39
- .doc(this.uid)
45
+ .doc(id)
40
-
46
+
41
- .get()
47
+ .get()
42
-
48
+
43
- .then((snapshot) => {
49
+ .then((snapshot) => {
44
-
50
+
45
- this.profileDeta = snapshot.data();
51
+ this.userDatas.push(snapshot.data());
52
+
46
-
53
+ });
54
+
55
+ });
56
+
57
+ },
58
+
59
+ methods: {
60
+
61
+ scrollBottom() {
62
+
47
- console.log(this.profileDeta);
63
+ this.$nextTick(() => {
64
+
65
+ window.scrollTo(0, document.body.clientHeight);
48
66
 
49
67
  });
50
68
 
69
+ },
70
+
71
+ childAdded(snap) {
72
+
73
+ const message = snap.val();
74
+
75
+ if (!this.userIds.includes(message.userid))
76
+
77
+ this.userIds.push(message.userid);
78
+
79
+ this.chat.push({
80
+
81
+ key: snap.key,
82
+
83
+ name: message.name,
84
+
85
+ image: message.image,
86
+
87
+ message: message.message,
88
+
89
+ userid: message.userid,
90
+
91
+ time: message.time,
92
+
93
+ });
94
+
95
+ this.scrollBottom();
96
+
97
+ },
98
+
99
+ returnUserData(id) {
100
+
101
+ const userData = this.userDatas.filter((user) => user.id === id);
102
+
103
+ return userData;
104
+
105
+ },
106
+
107
+ ```
108
+
109
+ ![イメージ説明](79a34bbc045513243e6ade46249980a2.png)
110
+
111
+
112
+
113
+
114
+
115
+ 上記のように記載して表示させて、filterメソッドを使ってidからユーザーを取り出そうと考えていますが、
116
+
117
+ どのようにここから記述したらいいのかわからない状況です。
118
+
119
+
120
+
121
+ 分かる方いらっしゃいましたらお力添えをいただきたいです。
122
+
123
+ お手数おかけしますが、よろしくおがいいたします。
124
+
125
+
126
+
127
+
128
+
129
+ ```html
130
+
131
+ <template>
132
+
133
+ <div class="chat">
134
+
135
+ <h1 class="chat-tll flex">
136
+
137
+ <div class="flash neon">Chat Room</div>
138
+
139
+ <router-link :to="`/board/${this.uid}`" class="back-btn">
140
+
141
+ <img
142
+
143
+ src="../assets/戻る.jpg"
144
+
145
+ alt="チャット終了"
146
+
147
+ class="back-btn-icon"
148
+
149
+ />
150
+
151
+ </router-link>
152
+
153
+ </h1>
154
+
155
+ <!--Firebase から取得したリストを描画-->
156
+
157
+ <transition-group name="chat" tag="div" class="list content">
158
+
159
+ <!--chatの中の{ key, name, image, message ,userid }をそれぞれ取得-->
160
+
161
+ <section
162
+
163
+ v-for="{ key, name, image, message, userid, time } in chat"
164
+
165
+ :key="key"
166
+
167
+ >
168
+
169
+ <div v-if="userid === user.uid" class="myitem flex">
170
+
171
+ {{ userDatas }}
172
+
173
+ <!-- 自身 -->
174
+
175
+ <!--「画像」の指定-->
176
+
177
+
178
+
179
+ <!--「名前」と「メッセージ」の指定-->
180
+
181
+ <div class="mydetail">
182
+
183
+ <div class="mytime">{{ $dayjs(time).format("hh:mm") }}</div>
184
+
185
+ <div @click.right.prevent="deleteMessage(key)" class="mymessage">
186
+
187
+ <nl2br tag="div" :text="message" />
188
+
189
+ </div>
190
+
191
+ </div>
192
+
193
+ <div class="myimage flex">
194
+
195
+ <img :src="image" width="40" height="40" />
196
+
197
+ <div class="myname">{{ name }}</div>
198
+
199
+ </div>
200
+
201
+ </div>
202
+
203
+ <div v-else class="otheritem flex">
204
+
205
+ <!-- 自身ではない -->
206
+
207
+ <!--「画像」の指定-->
208
+
209
+ <div class="otherimage flex">
210
+
211
+ <img :src="image" width="40" height="40" />
212
+
213
+ <div class="othername">name</div>
214
+
215
+ </div>
216
+
217
+ <!--「名前」と「メッセージ」の指定-->
218
+
219
+ <div class="otherdetail">
220
+
221
+ <div class="othermessage">
222
+
223
+ <nl2br tag="div" :text="message" />
224
+
225
+ </div>
226
+
227
+ <div class="othertime">{{ $dayjs(time).format("hh:mm") }}</div>
228
+
229
+ </div>
230
+
231
+ </div>
232
+
233
+ </section>
234
+
235
+ </transition-group>
236
+
237
+
238
+
239
+ <!-- 入力フォームの設定 -->
240
+
241
+ <div class="message-inner flex">
242
+
243
+ <form action @submit.prevent="doSend" class="form flex">
244
+
245
+ <textarea
246
+
247
+ v-model="input"
248
+
249
+ placeholder="メッセージを入力"
250
+
251
+ :disabled="!user.uid"
252
+
253
+ @keydown.enter.exact.prevent="doSend"
254
+
255
+ ></textarea>
256
+
257
+ <!-- ユーザーでなければ無効化 -->
258
+
259
+ <button type="submit" :disabled="!user.uid" class="send-button">
260
+
261
+ <img src="../assets/電球.jpg" class="send-img" alt="送信" />
262
+
263
+ </button>
264
+
265
+ </form>
266
+
267
+ </div>
268
+
269
+ </div>
270
+
271
+ </template>
272
+
273
+ ```
274
+
275
+
276
+
277
+ ```js
278
+
279
+ <script>
280
+
281
+ import firebase from "firebase";
282
+
283
+ import Nl2br from "vue-nl2br";
284
+
285
+ import Vue from "vue";
286
+
287
+ // 改行を <br> タグに変換するモジュール
288
+
289
+ import dayjs from "dayjs";
290
+
291
+
292
+
293
+ dayjs.locale("ja");
294
+
295
+ Vue.prototype.$dayjs = dayjs;
296
+
297
+
298
+
299
+ export default {
300
+
301
+ components: { Nl2br },
302
+
303
+ data() {
304
+
305
+ return {
306
+
307
+ user: {}, // ユーザー情報
308
+
309
+ chat: [], // 取得したメッセージを入れる配列
310
+
311
+ input: "", // 入力したメッセージ
312
+
313
+ usersData: [],
314
+
315
+ profileDeta: {},
316
+
317
+ userIds: [],
318
+
319
+ userDatas: [],
320
+
321
+ };
322
+
51
323
  },
52
324
 
325
+ created() {
326
+
327
+ firebase.auth().onAuthStateChanged((user) => {
328
+
329
+ // ログイン状態ならuserが取得できる
330
+
331
+ this.user = user ? user : {};
332
+
333
+ //firebase.database()で以下のデータベースの読み書きを行う。
334
+
335
+ const ref_message = firebase.database().ref(this.$route.params.id);
336
+
337
+ //[router.vue]にて「/ ~ /:id」と指定しルートがマッチした時、
338
+
339
+ //この動的セグメントの値は全てのコンポーネント内で this.$route.params として利用可能になる。
340
+
341
+ if (user) {
342
+
343
+ this.chat = [];
344
+
345
+ // limitToLast(10)で並べ替えられた「message」の最後の10個を取得。
346
+
347
+ // on()は、message に変更があったときのハンドラを登録
348
+
349
+ //child_addedは、データベースのリスト「message」を取得。
350
+
351
+ ref_message.limitToLast(10).on("child_added", this.childAdded);
352
+
353
+ } else {
354
+
355
+ // message に変更があったときのハンドラを解除
356
+
357
+ ref_message.limitToLast(10).off("child_added", this.childAdded);
358
+
359
+ }
360
+
361
+ });
362
+
363
+ const currentUser = firebase.auth().currentUser;
364
+
365
+ //現在ログインしているユーザーを取得
366
+
367
+ this.uid = currentUser.uid;
368
+
369
+
370
+
371
+ this.userIds.forEach((id) => {
372
+
373
+ firebase
374
+
375
+ .firestore()
376
+
377
+ .collection("users")
378
+
379
+ .doc(id)
380
+
381
+ .get()
382
+
383
+ .then((snapshot) => {
384
+
385
+ this.userDatas.push(snapshot.data());
386
+
387
+ });
388
+
389
+ });
390
+
391
+ console.log(this.userIds);
392
+
393
+ },
394
+
395
+ methods: {
396
+
397
+ // スクロール位置を一番下に移動
398
+
399
+ scrollBottom() {
400
+
401
+ this.$nextTick(() => {
402
+
403
+ //this.$nextTickは、再描画を待つ。
404
+
405
+ //絶対値からbody要素の高さを取得
406
+
407
+ window.scrollTo(0, document.body.clientHeight);
408
+
409
+ });
410
+
411
+ },
412
+
413
+ childAdded(snap) {
414
+
415
+ //snapshotとは、ある時点における特定のデータベース参照にあるデータの全体像を写し取ったもの
416
+
417
+ //childAdded:データベースからアイテムのリストを取得する関数
418
+
419
+ // 受け取ったメッセージをchatに追加
420
+
421
+ const message = snap.val();
422
+
423
+ if (!this.userIds.includes(message.userid))
424
+
425
+ this.userIds.push(message.userid);
426
+
427
+ //イベントのときにデータベース内の「message」データを取得。
428
+
429
+ // データベースに新しい要素が追加されると随時呼び出される
430
+
431
+ this.chat.push({
432
+
433
+ key: snap.key,
434
+
435
+ name: message.name,
436
+
437
+ image: message.image,
438
+
439
+ message: message.message,
440
+
441
+ userid: message.userid,
442
+
443
+ time: message.time,
444
+
445
+ });
446
+
447
+ this.scrollBottom();
448
+
449
+ //スクロールの一番下に追加。
450
+
451
+ },
452
+
453
+ doSend() {
454
+
455
+ const time = time;
456
+
457
+ if (this.user.uid && this.input.length) {
458
+
459
+ // firebaseに書き込まれたメッセージを追加
460
+
461
+ firebase
462
+
463
+ .database()
464
+
465
+ .ref(this.$route.params.id)
466
+
467
+ .push(
468
+
469
+ {
470
+
471
+ message: this.input,
472
+
473
+ name: this.user.displayName,
474
+
475
+ image: this.user.photoURL,
476
+
477
+ userid: this.user.uid,
478
+
479
+ time: firebase.database.ServerValue.TIMESTAMP,
480
+
481
+ },
482
+
483
+
484
+
485
+ () => {
486
+
487
+ this.input = ""; // フォームを空にする
488
+
489
+ }
490
+
491
+ );
492
+
493
+ }
494
+
495
+ },
496
+
497
+ returnUserData(id) {
498
+
499
+ const userData = this.userDatas.filter((user) => user.id === id);
500
+
501
+ return userData;
502
+
503
+ },
504
+
505
+ deleteMessage(key) {
506
+
507
+ firebase
508
+
509
+ .database()
510
+
511
+ .ref(this.$route.params.id + "/" + key)
512
+
513
+ .remove();
514
+
515
+ console.log(key);
516
+
517
+ this.$swal({
518
+
519
+ title: "内容確認",
520
+
521
+ text: "メッセージを削除しますか?",
522
+
523
+ icon: "warning",
524
+
525
+ buttons: true,
526
+
527
+ dangerMode: true,
528
+
529
+ }).then((willDelete) => {
530
+
531
+ if (willDelete) {
532
+
533
+ this.$swal("メッセージを削除しました", {
534
+
535
+ icon: "success",
536
+
537
+ });
538
+
539
+ this.$router.go({
540
+
541
+ path: `/chat/${this.$route.params.id}`,
542
+
543
+ force: true,
544
+
545
+ });
546
+
547
+ } else {
548
+
549
+ this.$swal("キャンセルしました。");
550
+
551
+ }
552
+
553
+ });
554
+
555
+ },
556
+
557
+ },
558
+
559
+ };
560
+
561
+ </script>
562
+
53
563
  ```
54
-
55
-
56
-
57
- 上記のように記載して表示させようとしましたが、これではログイン中のユーザー情報しか表示されず、
58
-
59
- その他のユーザーは表示されない状態です。。
60
-
61
-
62
-
63
- チャットルームにいるユーザーの情報とFirestoreのユーザー情報を紐付けて
64
-
65
- それぞれ名前とアイコンを表示させたいです。
66
-
67
-
68
-
69
- 分かる方いらっしゃいましたらお力添えをいただきたいです。
70
-
71
- お手数おかけしますが、よろしくおがいいたします。
72
-
73
-
74
-
75
-
76
-
77
- ```html
78
-
79
- <template>
80
-
81
- <div class="chat">
82
-
83
- <h1 class="chat-tll flex">
84
-
85
- <div class="flash neon">Chat Room</div>
86
-
87
- <router-link :to="`/board/${this.uid}`" class="back-btn">
88
-
89
- <img
90
-
91
- src="../assets/戻る.jpg"
92
-
93
- alt="チャット終了"
94
-
95
- class="back-btn-icon"
96
-
97
- />
98
-
99
- </router-link>
100
-
101
- </h1>
102
-
103
- <!--Firebase から取得したリストを描画-->
104
-
105
- <transition-group name="chat" tag="div" class="list content">
106
-
107
- <!--chatの中の{ key, name, image, message ,userid }をそれぞれ取得-->
108
-
109
- <section
110
-
111
- v-for="{ key, name, image, message, userid, time } in chat"
112
-
113
- :key="key"
114
-
115
- >
116
-
117
- <div v-if="userid === user.uid" class="myitem flex">
118
-
119
- <!-- 自身 -->
120
-
121
- <!--「画像」の指定-->
122
-
123
-
124
-
125
- <!--「名前」と「メッセージ」の指定-->
126
-
127
- <div class="mydetail">
128
-
129
- <div class="mytime">{{ $dayjs(time).format("hh:mm") }}</div>
130
-
131
- <div @click.right.prevent="deleteMessage(key)" class="mymessage">
132
-
133
- <nl2br tag="div" :text="message" />
134
-
135
- </div>
136
-
137
- </div>
138
-
139
- <div class="myimage flex">
140
-
141
- <img :src="image" width="40" height="40" />
142
-
143
- <div class="myname">{{ profileDeta.name }}</div>
144
-
145
- </div>
146
-
147
- </div>
148
-
149
- <div v-else class="otheritem flex">
150
-
151
- <!-- 自身ではない -->
152
-
153
- <!--「画像」の指定-->
154
-
155
- <div class="otherimage flex">
156
-
157
- <img :src="image" width="40" height="40" />
158
-
159
- <div class="othername">name</div>
160
-
161
- </div>
162
-
163
- <!--「名前」と「メッセージ」の指定-->
164
-
165
- <div class="otherdetail">
166
-
167
- <div class="othermessage">
168
-
169
- <nl2br tag="div" :text="message" />
170
-
171
- </div>
172
-
173
- <div class="othertime">{{ $dayjs(time).format("hh:mm") }}</div>
174
-
175
- </div>
176
-
177
- </div>
178
-
179
- </section>
180
-
181
- </transition-group>
182
-
183
- ```
184
-
185
-
186
-
187
- ```js
188
-
189
- <script>
190
-
191
- import firebase from "firebase";
192
-
193
- import Nl2br from "vue-nl2br";
194
-
195
- import Vue from "vue";
196
-
197
- // 改行を <br> タグに変換するモジュール
198
-
199
- import dayjs from "dayjs";
200
-
201
-
202
-
203
- dayjs.locale("ja");
204
-
205
- Vue.prototype.$dayjs = dayjs;
206
-
207
-
208
-
209
- export default {
210
-
211
- components: { Nl2br },
212
-
213
- data() {
214
-
215
- return {
216
-
217
- user: {}, // ユーザー情報
218
-
219
- chat: [], // 取得したメッセージを入れる配列
220
-
221
- input: "", // 入力したメッセージ
222
-
223
- usersData: [],
224
-
225
- profileDeta: {},
226
-
227
- };
228
-
229
- },
230
-
231
- created() {
232
-
233
- firebase.auth().onAuthStateChanged((user) => {
234
-
235
- // ログイン状態ならuserが取得できる
236
-
237
- this.user = user ? user : {};
238
-
239
- //firebase.database()で以下のデータベースの読み書きを行う。
240
-
241
- const ref_message = firebase.database().ref(this.$route.params.id);
242
-
243
- //[router.vue]にて「/ ~ /:id」と指定しルートがマッチした時、
244
-
245
- //この動的セグメントの値は全てのコンポーネント内で this.$route.params として利用可能になる。
246
-
247
- if (user) {
248
-
249
- this.chat = [];
250
-
251
- // limitToLast(10)で並べ替えられた「message」の最後の10個を取得。
252
-
253
- // on()は、message に変更があったときのハンドラを登録
254
-
255
- //child_addedは、データベースのリスト「message」を取得。
256
-
257
- ref_message.limitToLast(10).on("child_added", this.childAdded);
258
-
259
- } else {
260
-
261
- // message に変更があったときのハンドラを解除
262
-
263
- ref_message.limitToLast(10).off("child_added", this.childAdded);
264
-
265
- }
266
-
267
- });
268
-
269
- const currentUser = firebase.auth().currentUser;
270
-
271
- //現在ログインしているユーザーを取得
272
-
273
- this.uid = currentUser.uid;
274
-
275
- firebase
276
-
277
- .firestore()
278
-
279
- .collection("users")
280
-
281
- .doc(currentUser.uid)
282
-
283
- .get();
284
-
285
- firebase
286
-
287
- .firestore()
288
-
289
- .collection("users")
290
-
291
- .doc(this.uid)
292
-
293
- .get()
294
-
295
- .then((snapshot) => {
296
-
297
- this.profileDeta = snapshot.data();
298
-
299
- console.log(this.profileDeta);
300
-
301
- });
302
-
303
- },
304
-
305
- methods: {
306
-
307
- // スクロール位置を一番下に移動
308
-
309
- scrollBottom() {
310
-
311
- this.$nextTick(() => {
312
-
313
- //this.$nextTickは、再描画を待つ。
314
-
315
- //絶対値からbody要素の高さを取得
316
-
317
- window.scrollTo(0, document.body.clientHeight);
318
-
319
- });
320
-
321
- },
322
-
323
- childAdded(snap) {
324
-
325
- //snapshotとは、ある時点における特定のデータベース参照にあるデータの全体像を写し取ったもの
326
-
327
- //childAdded:データベースからアイテムのリストを取得する関数
328
-
329
- // 受け取ったメッセージをchatに追加
330
-
331
- const message = snap.val();
332
-
333
- //イベントのときにデータベース内の「message」データを取得。
334
-
335
- // データベースに新しい要素が追加されると随時呼び出される
336
-
337
- this.chat.push({
338
-
339
- key: snap.key,
340
-
341
- name: message.name,
342
-
343
- image: message.image,
344
-
345
- message: message.message,
346
-
347
- userid: message.userid,
348
-
349
- time: message.time,
350
-
351
- });
352
-
353
- this.scrollBottom();
354
-
355
- //スクロールの一番下に追加。
356
-
357
- },
358
-
359
- doSend() {
360
-
361
- const time = time;
362
-
363
- if (this.user.uid && this.input.length) {
364
-
365
- // firebaseに書き込まれたメッセージを追加
366
-
367
- firebase
368
-
369
- .database()
370
-
371
- .ref(this.$route.params.id)
372
-
373
- .push(
374
-
375
- {
376
-
377
- message: this.input,
378
-
379
- name: this.user.displayName,
380
-
381
- image: this.user.photoURL,
382
-
383
- userid: this.user.uid,
384
-
385
- time: firebase.database.ServerValue.TIMESTAMP,
386
-
387
- },
388
-
389
-
390
-
391
- () => {
392
-
393
- this.input = ""; // フォームを空にする
394
-
395
- }
396
-
397
- );
398
-
399
- }
400
-
401
- },
402
-
403
- deleteMessage() {
404
-
405
- firebase
406
-
407
- .database()
408
-
409
- .ref()
410
-
411
- .remove();
412
-
413
- this.$swal({
414
-
415
- title: "内容確認",
416
-
417
- text: "メッセージを削除しますか?",
418
-
419
- icon: "warning",
420
-
421
- buttons: true,
422
-
423
- dangerMode: true,
424
-
425
- }).then((willDelete) => {
426
-
427
- if (willDelete) {
428
-
429
- this.$swal("メッセージを削除しました", {
430
-
431
- icon: "success",
432
-
433
- });
434
-
435
- this.$router.go({
436
-
437
- path: `/chat/${this.$route.params.id}`,
438
-
439
- force: true,
440
-
441
- });
442
-
443
- } else {
444
-
445
- this.$swal("キャンセルしました。");
446
-
447
- }
448
-
449
- });
450
-
451
- },
452
-
453
- },
454
-
455
- };
456
-
457
- </script>
458
-
459
- ```
460
-
461
-
462
-
463
- #追記
464
-
465
-
466
-
467
- authenticationでuidは管理しておりますが、現状名前とアイコンは設定していないです。
468
-
469
-
470
-
471
- [Firebase でユーザーを管理する](https://firebase.google.com/docs/auth/web/manage-users?hl=ja)
472
-
473
-
474
-
475
- 下記は公式より抜粋しただけです。
476
-
477
- ```
478
-
479
- var user = firebase.auth().currentUser;
480
-
481
- var name, email, photoUrl, uid, emailVerified;
482
-
483
-
484
-
485
- if (user != null) {
486
-
487
- name = user.displayName;
488
-
489
- email = user.email;
490
-
491
- photoUrl = user.photoURL;
492
-
493
- emailVerified = user.emailVerified;
494
-
495
- uid = user.uid;
496
-
497
- }
498
-
499
- ```
500
-
501
-
502
-
503
- 名前やアイコンは別componentsでmypage.vueというコンポーネントを作成して、
504
-
505
- そちらで下記のようにプロフィール編集欄を設けてユーザー情報を管理してます。
506
-
507
-
508
-
509
- ```ここに言語を入力
510
-
511
- methods: {
512
-
513
- updateBtn() {
514
-
515
- firebase
516
-
517
- .firestore()
518
-
519
- .collection("users")
520
-
521
- .doc(this.$route.params.uid)
522
-
523
- //現在のURLのパラメーターを取得。
524
-
525
- .set(
526
-
527
- {
528
-
529
- name: this.name,
530
-
531
- sex: this.sex,
532
-
533
- age: this.age,
534
-
535
- access: this.access,
536
-
537
- selfpr: this.selfpr,
538
-
539
- profession: this.profession,
540
-
541
- genre: this.genre,
542
-
543
- favMovie: this.favMovie,
544
-
545
- time: firebase.firestore.FieldValue.serverTimestamp(),
546
-
547
- //サーバ側で値設定
548
-
549
- },
550
-
551
- { merge: true }
552
-
553
- );
554
-
555
-
556
-
557
-   created() {
558
-
559
- const currentUser = firebase.auth().currentUser;
560
-
561
- if (currentUser) {
562
-
563
- firebase
564
-
565
- .firestore()
566
-
567
- .collection("users")
568
-
569
- .doc(this.$route.params.uid)
570
-
571
- .get()
572
-
573
- .then((snapshot) => {
574
-
575
- this.profileData = snapshot.data();
576
-
577
- this.name = this.profileData.name || "";
578
-
579
- this.sex = this.profileData.sex || "";
580
-
581
- this.age = this.profileData.age || "";
582
-
583
- this.access = this.profileData.access || "";
584
-
585
- this.selfpr = this.profileData.selfpr || "";
586
-
587
- this.profession = this.profileData.profession || "";
588
-
589
- this.uploadedImage = this.profileData.uploadedImage || "";
590
-
591
- this.genre = this.profileData.genre || "";
592
-
593
- this.favMovie = this.profileData.favMovie || "";
594
-
595
- });
596
-
597
- }
598
-
599
- ```
600
-
601
-
602
-
603
- firestoreのユーザー情報をそのままrealtime detabaseで作成したチャットルームで
604
-
605
- それぞれのユーザー情報を取得し表示したいと考えております。
606
-
607
-
608
-
609
- 引き続き分かる方いらっしゃいましたらお力添えを頂きたいです。

1

追記を追加しました。

2021/05/18 12:40

投稿

TMTN
TMTN

スコア53

test CHANGED
File without changes
test CHANGED
@@ -457,3 +457,153 @@
457
457
  </script>
458
458
 
459
459
  ```
460
+
461
+
462
+
463
+ #追記
464
+
465
+
466
+
467
+ authenticationでuidは管理しておりますが、現状名前とアイコンは設定していないです。
468
+
469
+
470
+
471
+ [Firebase でユーザーを管理する](https://firebase.google.com/docs/auth/web/manage-users?hl=ja)
472
+
473
+
474
+
475
+ 下記は公式より抜粋しただけです。
476
+
477
+ ```
478
+
479
+ var user = firebase.auth().currentUser;
480
+
481
+ var name, email, photoUrl, uid, emailVerified;
482
+
483
+
484
+
485
+ if (user != null) {
486
+
487
+ name = user.displayName;
488
+
489
+ email = user.email;
490
+
491
+ photoUrl = user.photoURL;
492
+
493
+ emailVerified = user.emailVerified;
494
+
495
+ uid = user.uid;
496
+
497
+ }
498
+
499
+ ```
500
+
501
+
502
+
503
+ 名前やアイコンは別componentsでmypage.vueというコンポーネントを作成して、
504
+
505
+ そちらで下記のようにプロフィール編集欄を設けてユーザー情報を管理してます。
506
+
507
+
508
+
509
+ ```ここに言語を入力
510
+
511
+ methods: {
512
+
513
+ updateBtn() {
514
+
515
+ firebase
516
+
517
+ .firestore()
518
+
519
+ .collection("users")
520
+
521
+ .doc(this.$route.params.uid)
522
+
523
+ //現在のURLのパラメーターを取得。
524
+
525
+ .set(
526
+
527
+ {
528
+
529
+ name: this.name,
530
+
531
+ sex: this.sex,
532
+
533
+ age: this.age,
534
+
535
+ access: this.access,
536
+
537
+ selfpr: this.selfpr,
538
+
539
+ profession: this.profession,
540
+
541
+ genre: this.genre,
542
+
543
+ favMovie: this.favMovie,
544
+
545
+ time: firebase.firestore.FieldValue.serverTimestamp(),
546
+
547
+ //サーバ側で値設定
548
+
549
+ },
550
+
551
+ { merge: true }
552
+
553
+ );
554
+
555
+
556
+
557
+   created() {
558
+
559
+ const currentUser = firebase.auth().currentUser;
560
+
561
+ if (currentUser) {
562
+
563
+ firebase
564
+
565
+ .firestore()
566
+
567
+ .collection("users")
568
+
569
+ .doc(this.$route.params.uid)
570
+
571
+ .get()
572
+
573
+ .then((snapshot) => {
574
+
575
+ this.profileData = snapshot.data();
576
+
577
+ this.name = this.profileData.name || "";
578
+
579
+ this.sex = this.profileData.sex || "";
580
+
581
+ this.age = this.profileData.age || "";
582
+
583
+ this.access = this.profileData.access || "";
584
+
585
+ this.selfpr = this.profileData.selfpr || "";
586
+
587
+ this.profession = this.profileData.profession || "";
588
+
589
+ this.uploadedImage = this.profileData.uploadedImage || "";
590
+
591
+ this.genre = this.profileData.genre || "";
592
+
593
+ this.favMovie = this.profileData.favMovie || "";
594
+
595
+ });
596
+
597
+ }
598
+
599
+ ```
600
+
601
+
602
+
603
+ firestoreのユーザー情報をそのままrealtime detabaseで作成したチャットルームで
604
+
605
+ それぞれのユーザー情報を取得し表示したいと考えております。
606
+
607
+
608
+
609
+ 引き続き分かる方いらっしゃいましたらお力添えを頂きたいです。