teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

修正

2021/05/21 03:26

投稿

TMTN
TMTN

スコア53

title CHANGED
File without changes
body CHANGED
@@ -7,50 +7,32 @@
7
7
 
8
8
  そこで以下のようにifを使って投稿した際に、投稿自体に投稿者が分かるようにuidを取得できるようにしているので
9
9
 
10
- 「現在のログイン中のユーザー(this.$route.params.uid)と
10
+ 「現在のログイン中のユーザー(this.uid)と
11
+ ```ここに言語を入力
12
+ const currentUser = firebase.auth().currentUser;
11
- 「postsを参照して、投稿者の(uid)」
13
+ this.uid = currentUser.uid;
14
+ ```
12
15
 
13
- 上記2つが合致した場合にアラートが出るようにしたいと考えていますが、
14
- 「postsを参照して、投稿者の(uid)」を参照するやり方がわからず困っております。。
16
+ 「postsを参照して、投稿者の(this.list.uid)」
17
+ ```ここに言語を入力
18
+ props: {
19
+ list: {
20
+ type: Object,
21
+ },
22
+ index: {
23
+ type: Number,
24
+ },
25
+ ```
15
26
 
16
- ![画像](cd3db88b99da456e4e514b9566e5dbaf.png)
17
-
18
- 青で囲っているuid部分を取得する際には、どのように記述したら良いのでしょうか。。
19
-
20
27
  ![イメージ説明](dbacf363a32e966c50b53d1b2c66256d.png)
21
28
 
22
- ```js
23
- created() {
24
- firebase
25
- .firestore()
26
- .collection("posts")
27
- .doc(this.list.id)
28
- .get()
29
- ```
30
- 現状、上記this.list.idにてドキュメントidまで取得はできおります。
29
+ props受けったuidを使っ投稿者と認識させています。
31
30
 
31
+ if文で互いのuidを認識させることはできたのですが、
32
- 公式書いいるようにget()使ってuidを取得して「this.userid」に格納し
32
+ なぜかアラートにてキャンセルをしても対象の投稿が消えしまいます・・
33
- if (this.userid == this.$route.params.uid) を使用しようと考えています。
34
33
 
35
- [Cloud Firestore でデータを取得す](https://firebase.google.com/docs/firestore/query-data/get-data?hl=ja)
34
+ if分の位置が誤っていのでしょうか・・
36
35
 
37
- ```js
38
- created() {
39
- firebase
40
- .firestore()
41
- .collection("posts")
42
- .doc(this.list.id)
43
- .get()
44
- .then((doc) => {
45
- if (doc.exists) {
46
- this.userid.push(doc.data().uid);
47
- }
48
- });
49
- },
50
- ```
51
-
52
- このget()以降がどのように記述したらuidを取ってこれるのか分からない状況です。。
53
-
54
36
  また、もし他に良い方法があれば教えていただけると幸いです。
55
37
 
56
38
  よろしくお願い致します。
@@ -60,33 +42,11 @@
60
42
  ```
61
43
 
62
44
  ```js
63
- created() {
64
- firebase
65
- .firestore()
66
- .collection("posts")
67
- .doc(this.list.id)
68
- .get()
69
- .then((doc) => {
70
- if (doc.exists) {
71
- this.userid.push(doc.data().uid);
72
- }
73
- });
74
- },
75
- methods: {
76
- savePost() {
77
- firebase
78
- .firestore()
79
- .collection("users")
80
- .doc(this.$route.params.uid)
81
- .collection("bookmarks")
82
- .doc(this.$route.params.uid)
83
- .set({
84
- uid: this.$route.params.uid,
85
- list: this.list,
86
- });
87
- },
88
- deletePost(id) {
45
+ deletePost() {
46
+ const currentUser = firebase.auth().currentUser;
47
+ this.uid = currentUser.uid;
48
+
89
- if (this.userid == this.$route.params.uid) {
49
+ if (this.list.uid == this.uid) {
90
50
  firebase
91
51
  .firestore()
92
52
  .collection("posts")
@@ -98,8 +58,8 @@
98
58
  icon: "warning",
99
59
  buttons: true,
100
60
  dangerMode: true,
61
+ })
101
- }).then((willDelete) => {
62
+ .then(() => {
102
- if (willDelete) {
103
63
  this.$swal("投稿を削除しました", {
104
64
  icon: "success",
105
65
  });
@@ -107,11 +67,12 @@
107
67
  path: `/board/${this.$route.params.uid}`,
108
68
  force: true,
109
69
  });
70
+ })
110
- } else {
71
+ .catch(() => {
111
72
  this.$swal("キャンセルしました。");
73
+ });
112
- }
74
+ }
113
- });
114
- // }
115
75
  },
76
+ },
116
77
  },
117
78
  ```

5

修正

2021/05/21 03:26

投稿

TMTN
TMTN

スコア53

title CHANGED
File without changes
body CHANGED
@@ -17,9 +17,40 @@
17
17
 
18
18
  青で囲っているuid部分を取得する際には、どのように記述したら良いのでしょうか。。
19
19
 
20
+ ![イメージ説明](dbacf363a32e966c50b53d1b2c66256d.png)
21
+
22
+ ```js
23
+ created() {
24
+ firebase
25
+ .firestore()
26
+ .collection("posts")
27
+ .doc(this.list.id)
28
+ .get()
29
+ ```
30
+ 現状、上記this.list.idにてドキュメントidまで取得はできております。
31
+
20
- 公式に書いているようにget()を使うとは思うのですが・・
32
+ 公式に書いているようにget()を使ってuidを取得して「this.userid」に格納して
33
+ if (this.userid == this.$route.params.uid) を使用しようと考えています。
34
+
21
35
  [Cloud Firestore でデータを取得する](https://firebase.google.com/docs/firestore/query-data/get-data?hl=ja)
22
36
 
37
+ ```js
38
+ created() {
39
+ firebase
40
+ .firestore()
41
+ .collection("posts")
42
+ .doc(this.list.id)
43
+ .get()
44
+ .then((doc) => {
45
+ if (doc.exists) {
46
+ this.userid.push(doc.data().uid);
47
+ }
48
+ });
49
+ },
50
+ ```
51
+
52
+ このget()以降がどのように記述したらuidを取ってこれるのか分からない状況です。。
53
+
23
54
  また、もし他に良い方法があれば教えていただけると幸いです。
24
55
 
25
56
  よろしくお願い致します。

4

内容訂正。

2021/05/20 12:28

投稿

TMTN
TMTN

スコア53

title CHANGED
File without changes
body CHANGED
@@ -25,7 +25,7 @@
25
25
  よろしくお願い致します。
26
26
 
27
27
  ```html
28
- <button class="hide-btn" @click="deletePost(id)">×</button>
28
+ <button class="hide-btn" @click="deletePost">×</button>
29
29
  ```
30
30
 
31
31
  ```js
@@ -33,12 +33,11 @@
33
33
  firebase
34
34
  .firestore()
35
35
  .collection("posts")
36
- .doc(this.$route.params.uid)
36
+ .doc(this.list.id)
37
37
  .get()
38
38
  .then((doc) => {
39
39
  if (doc.exists) {
40
40
  this.userid.push(doc.data().uid);
41
- console.log(doc.data());
42
41
  }
43
42
  });
44
43
  },
@@ -56,13 +55,12 @@
56
55
  });
57
56
  },
58
57
  deletePost(id) {
59
- // if (this.userid == this.$route.params.uid) {
58
+ if (this.userid == this.$route.params.uid) {
60
59
  firebase
61
60
  .firestore()
62
61
  .collection("posts")
63
- .doc()
62
+ .doc(this.list.id)
64
63
  .delete();
65
- console.log(id);
66
64
  this.$swal({
67
65
  title: "内容確認",
68
66
  text: "投稿を削除しますか?",

3

内容変更

2021/05/20 12:23

投稿

TMTN
TMTN

スコア53

title CHANGED
File without changes
body CHANGED
@@ -10,8 +10,8 @@
10
10
  「現在のログイン中のユーザー(this.$route.params.uid)と
11
11
  「postsを参照して、投稿者の(uid)」
12
12
 
13
- 上記2つが合致した場合にアラートが出るようにしたいと考えていますが、「postsを参照して、投稿者の(uid)」を
13
+ 上記2つが合致した場合にアラートが出るようにしたいと考えていますが、
14
- 参照するやり方がわからず困っております。。
14
+ 「postsを参照して、投稿者の(uid)」を参照するやり方がわからず困っております。。
15
15
 
16
16
  ![画像](cd3db88b99da456e4e514b9566e5dbaf.png)
17
17
 
@@ -25,31 +25,44 @@
25
25
  よろしくお願い致します。
26
26
 
27
27
  ```html
28
- <button class="hide-btn" @click="deletePost">×</button>
28
+ <button class="hide-btn" @click="deletePost(id)">×</button>
29
29
  ```
30
30
 
31
31
  ```js
32
32
  created() {
33
33
  firebase
34
+ .firestore()
35
+ .collection("posts")
36
+ .doc(this.$route.params.uid)
37
+ .get()
38
+ .then((doc) => {
39
+ if (doc.exists) {
40
+ this.userid.push(doc.data().uid);
41
+ console.log(doc.data());
42
+ }
43
+ });
44
+ },
45
+ methods: {
46
+ savePost() {
47
+ firebase
34
48
  .firestore()
35
- .collection("posts")
49
+ .collection("users")
36
50
  .doc(this.$route.params.uid)
51
+ .collection("bookmarks")
52
+ .doc(this.$route.params.uid)
37
- .get()
53
+ .set({
38
- .then((doc) => {
54
+ uid: this.$route.params.uid,
39
- if (doc.exists){
55
+ list: this.list,
40
-  const uid = doc.data(uid);
41
56
  });
42
- }
57
+ },
43
-
44
- deletePost() {
58
+ deletePost(id) {
45
-
46
- if (uid == this.$route.params.uid) {
59
+ // if (this.userid == this.$route.params.uid) {
47
- console.log(this.uid);
48
60
  firebase
49
61
  .firestore()
50
62
  .collection("posts")
51
- .doc(this.$route.params.uid)
63
+ .doc()
52
64
  .delete();
65
+ console.log(id);
53
66
  this.$swal({
54
67
  title: "内容確認",
55
68
  text: "投稿を削除しますか?",
@@ -69,6 +82,7 @@
69
82
  this.$swal("キャンセルしました。");
70
83
  }
71
84
  });
72
- }
85
+ // }
73
86
  },
87
+ },
74
88
  ```

2

js部修正

2021/05/18 12:46

投稿

TMTN
TMTN

スコア53

title CHANGED
File without changes
body CHANGED
@@ -29,12 +29,20 @@
29
29
  ```
30
30
 
31
31
  ```js
32
- deletePost() {
32
+ created() {
33
- const uid = firebase
33
+ firebase
34
34
  .firestore()
35
35
  .collection("posts")
36
+ .doc(this.$route.params.uid)
36
- .doc();
37
+ .get()
38
+ .then((doc) => {
39
+ if (doc.exists){
40
+  const uid = doc.data(uid);
41
+ });
42
+ }
37
43
 
44
+ deletePost() {
45
+
38
46
  if (uid == this.$route.params.uid) {
39
47
  console.log(this.uid);
40
48
  firebase

1

内容修正

2021/05/15 04:57

投稿

TMTN
TMTN

スコア53

title CHANGED
File without changes
body CHANGED
@@ -17,6 +17,9 @@
17
17
 
18
18
  青で囲っているuid部分を取得する際には、どのように記述したら良いのでしょうか。。
19
19
 
20
+ 公式に書いているようにget()を使うとは思うのですが・・
21
+ [Cloud Firestore でデータを取得する](https://firebase.google.com/docs/firestore/query-data/get-data?hl=ja)
22
+
20
23
  また、もし他に良い方法があれば教えていただけると幸いです。
21
24
 
22
25
  よろしくお願い致します。