質問編集履歴

1

コードの詳細を追記致しました。

2019/03/20 23:59

投稿

Khy
Khy

スコア118

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- firebaseには以下のようなusersコレクションがあり、その情報をcurrentUser.ageやcurrentUser.nameなどのような形で使いたいですがcreated()の中にどのように記述したらcurrentUserにオブジェクトの形でデータを入れれますでしょうか?
9
+ firebaseには以下のようなusersコレクションがあり、その情報をcurrentUser.ageやcurrentUser.nameなどのような形で使いたいです
10
10
 
11
11
 
12
12
 
@@ -26,6 +26,20 @@
26
26
 
27
27
 
28
28
 
29
+ 以下のようなコードで、console.log(doc.data());をした時にはきちんとコンソールにオブジェクトのデータが表示されるのですが、this.currentUser = doc.data();をすると次のエラーが出ます。
30
+
31
+
32
+
33
+ Error getting document: TypeError: Cannot set property 'a' of undefined
34
+
35
+
36
+
37
+ 原因がわかる方がいらっしゃいましたらお知恵を貸していただけると助かります。
38
+
39
+ またもっといい書き方があればお教えいただけると幸いです。
40
+
41
+ 宜しくお願い致します。
42
+
29
43
 
30
44
 
31
45
  ```ここに言語を入力
@@ -44,6 +58,8 @@
44
58
 
45
59
  return {
46
60
 
61
+ currentUserEmail: '',
62
+
47
63
  currentUser: {}, //ここに現在のユーザー情報を格納したい
48
64
 
49
65
  }
@@ -56,7 +72,25 @@
56
72
 
57
73
  if(firebase.auth().currentUser){
58
74
 
75
+ this.currentUserEmail = firebase.auth().currentUser.email; //ログイン中ユーザのメールアドレスを取得
76
+
77
+ var currentUserDoc = db.collection('users').doc(this.currentUserEmail); //ログイン中ユーザのドキュメントを代入
78
+
79
+
80
+
81
+    currentUserDoc.get().then(function(doc) {
82
+
83
+ if (doc.exists) {
84
+
85
+
86
+
87
+ console.log(doc.data());
88
+
59
-      //ここにどう書けばいいかわかりません...
89
+ this.currentUser = doc.data();
90
+
91
+
92
+
93
+ } else {...}
60
94
 
61
95
  }
62
96
 
@@ -64,10 +98,8 @@
64
98
 
65
99
  }
66
100
 
101
+ </script>
102
+
67
103
 
68
104
 
69
105
  ```
70
-
71
-
72
-
73
- お知恵を貸していただけると助かります。宜しくお願い致します。