質問編集履歴

4

app.use を追加

2021/06/20 10:52

投稿

hiro_ike
hiro_ike

スコア48

test CHANGED
File without changes
test CHANGED
@@ -140,6 +140,10 @@
140
140
 
141
141
  app.engine('jsx', require('express-react-views').createEngine())
142
142
 
143
+ //追記
144
+
145
+ app.use(express.urlencoded({ extended: true }))
146
+
143
147
 
144
148
 
145
149
  app.get('/', (req, res) => {

3

不要なコメント削除

2021/06/20 10:52

投稿

hiro_ike
hiro_ike

スコア48

test CHANGED
File without changes
test CHANGED
@@ -152,18 +152,12 @@
152
152
 
153
153
  /*
154
154
 
155
- Index
155
+ Indexにも追記
156
156
 
157
157
  */
158
158
 
159
159
  app.get('/pokemon', (req, res) => {
160
160
 
161
- // res.render('Index', {Pokemon: Pokemon});
162
-
163
- // })
164
-
165
- // app.get('/fruits/', (req, res) => {
166
-
167
161
  Pokemon.find({}, (err, foundPokemon)=>{
168
162
 
169
163
  if(err){

2

追記してみました

2021/06/20 08:42

投稿

hiro_ike
hiro_ike

スコア48

test CHANGED
File without changes
test CHANGED
@@ -110,6 +110,10 @@
110
110
 
111
111
  const Pokemon = require('./models/pokemon');
112
112
 
113
+
114
+
115
+
116
+
113
117
  const mongoose = require('mongoose');
114
118
 
115
119
  mongoose.connect(process.env.MONGO_URI, {
@@ -146,13 +150,51 @@
146
150
 
147
151
 
148
152
 
153
+ /*
154
+
155
+ Index
156
+
157
+ */
158
+
149
159
  app.get('/pokemon', (req, res) => {
150
160
 
151
- res.render('Index', {Pokemon: Pokemon});
161
+ // res.render('Index', {Pokemon: Pokemon});
162
+
152
-
163
+ // })
164
+
165
+ // app.get('/fruits/', (req, res) => {
166
+
167
+ Pokemon.find({}, (err, foundPokemon)=>{
168
+
169
+ if(err){
170
+
171
+ res.status(404).send({
172
+
173
+ msg: err.message
174
+
153
- })
175
+ })
176
+
154
-
177
+ } else {
178
+
155
-
179
+ res.render('Index', {
180
+
181
+ Pokemon: foundPokemon
182
+
183
+ })
184
+
185
+ }
186
+
187
+ })
188
+
189
+
190
+
191
+ })
192
+
193
+
194
+
195
+ /*
196
+
197
+ */
156
198
 
157
199
  app.get('/pokemon/new', (req, res) => {
158
200
 
@@ -174,9 +216,41 @@
174
216
 
175
217
 
176
218
 
219
+ /*
220
+
221
+ 追記
222
+
223
+ */
224
+
225
+ app.post('/pokemon', (req, res) =>{
226
+
227
+ Pokemon.create(req.body, (err, createdPokemon ) => {
228
+
229
+ if(err){
230
+
231
+ res.status(404).send({
232
+
233
+ msg: err.message
234
+
235
+ })
236
+
237
+ } else {
238
+
239
+ console.log(createdPokemon);
240
+
241
+ res.redirect('/pokemon')
242
+
243
+ }
244
+
245
+ })
246
+
247
+ })
248
+
249
+
250
+
177
251
  app.listen(PORT, () => {
178
252
 
179
- console.log('connected', PORT)
253
+ console.log('We in the building', PORT)
180
254
 
181
255
  })
182
256
 

1

MongoDB 追加

2021/06/20 08:36

投稿

hiro_ike
hiro_ike

スコア48

test CHANGED
File without changes
test CHANGED
File without changes