質問編集履歴

2

追記

2020/08/25 17:11

投稿

maskmelon
maskmelon

スコア63

test CHANGED
File without changes
test CHANGED
@@ -78,6 +78,8 @@
78
78
 
79
79
  ###追記
80
80
 
81
+ 最終的に以下のように実装することができました。
82
+
81
83
  ```JavaScript
82
84
 
83
85
  const commentInput = document.getElementById('comment-input');
@@ -91,6 +93,28 @@
91
93
  const currentUrl = window.location.href;
92
94
 
93
95
  const questionId = currentUrl.split('http://localhost:3000/question/')[1].split('/result')[0];
96
+
97
+
98
+
99
+ fetch(`/question/${questionId}/result/json`).then((response) => {
100
+
101
+ response.json().then((result) => {
102
+
103
+ const { postedComments } = result;
104
+
105
+ postedComments.forEach((comment) => {
106
+
107
+ const p = document.createElement('p');
108
+
109
+ p.innerHTML = comment.content;
110
+
111
+ commentBox.appendChild(p);
112
+
113
+ });
114
+
115
+ });
116
+
117
+ });
94
118
 
95
119
 
96
120
 
@@ -108,25 +132,17 @@
108
132
 
109
133
  body: JSON.stringify({ content: commentInput.value, questionId }),
110
134
 
111
- }).then(() => {
135
+ }).then((response) => {
112
136
 
113
- fetch(`/question/${questionId}/result/json`).then((response) => {
137
+ response.json().then((result) => {
114
138
 
115
- response.json().then((result) => {
139
+ const { populatedComment } = result;
116
140
 
117
- const { postedComments } = result; // 全てのコメントが入っている
141
+ const p = document.createElement('p');
118
142
 
119
- postedComments.forEach((comment) => {
143
+ p.innerHTML = populatedComment.content;
120
144
 
121
- const p = document.createElement('p');
122
-
123
- p.innerHTML = comment.content;
124
-
125
- commentBox.appendChild(p);
145
+ commentBox.prepend(p);
126
-
127
- });
128
-
129
- });
130
146
 
131
147
  });
132
148
 

1

コードの追加

2020/08/25 17:11

投稿

maskmelon
maskmelon

スコア63

test CHANGED
File without changes
test CHANGED
@@ -73,3 +73,67 @@
73
73
 
74
74
 
75
75
  ```
76
+
77
+
78
+
79
+ ###追記
80
+
81
+ ```JavaScript
82
+
83
+ const commentInput = document.getElementById('comment-input');
84
+
85
+ const commentSubmit = document.getElementById('comment-submit');
86
+
87
+ const commentBox = document.getElementById('comment-box');
88
+
89
+
90
+
91
+ const currentUrl = window.location.href;
92
+
93
+ const questionId = currentUrl.split('http://localhost:3000/question/')[1].split('/result')[0];
94
+
95
+
96
+
97
+ commentSubmit.onclick = () => {
98
+
99
+ fetch('/comment/create', {
100
+
101
+ method: 'POST',
102
+
103
+ headers: {
104
+
105
+ 'Content-Type': 'application/json',
106
+
107
+ },
108
+
109
+ body: JSON.stringify({ content: commentInput.value, questionId }),
110
+
111
+ }).then(() => {
112
+
113
+ fetch(`/question/${questionId}/result/json`).then((response) => {
114
+
115
+ response.json().then((result) => {
116
+
117
+ const { postedComments } = result; // 全てのコメントが入っている
118
+
119
+ postedComments.forEach((comment) => {
120
+
121
+ const p = document.createElement('p');
122
+
123
+ p.innerHTML = comment.content;
124
+
125
+ commentBox.appendChild(p);
126
+
127
+ });
128
+
129
+ });
130
+
131
+ });
132
+
133
+ });
134
+
135
+ };
136
+
137
+
138
+
139
+ ```