0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

expressでCRUDのコードを写経していたときに「TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined.」で一日ハマっていた件

Posted at

expressの「TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined.」というエラーについて

結論(どうすればよいか)

app.use(express.json());

を index.js に追加しろ

ちなみに、

app.use(express.json);

ではない
なぜかは知らん

エラー内容

TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined.

ソース

※もう直しているので再現はしません
github
https://github.com/2f0833e717/node-docker

実際のコンソール出力内容

> node-docker@1.0.0 dev
> nodemon -L index.js

[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
listening on port 3000
succesfully connected to DB
/app/controllers/authController.js:6
  const { username, password } = req.body;
          ^

TypeError: Cannot destructure property 'username' of 'req.body' as it is undefined.
    at exports.signUp (/app/controllers/authController.js:6:11)
    at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
    at next (/app/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/app/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
    at /app/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/app/node_modules/express/lib/router/index.js:346:12)
    at next (/app/node_modules/express/lib/router/index.js:280:10)
    at Function.handle (/app/node_modules/express/lib/router/index.js:175:3)
    at router (/app/node_modules/express/lib/router/index.js:47:12)
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`

余談その1 写経の元ネタ

[Youtube] [Google自動翻訳]
Dockerを学ぶ-Node.jsとExpressを使用したDevOps

余談その2 この辺がいるっぽい気がしたが要らんかった(順不同、適当)


const bodyparser = require("body-parser");

app.use(express.urlencoded({ extended: true }));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));

      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false, //これは非推奨となっている

余談の余談
GW、皆さんいかがお過ごしでしょうか。やろうやろうと思って一生やらなかったDocker周りの勉強中にハマって一晩寝かしたものの、エラー内容をそのままググったら、結局ちゃんと見たらそのまんまだった系のトラブルシュートとして本件を成仏するためQiitaに投稿。この手のネタ記事は今どきはZennじゃなくてQiitaかもしれない英語分からないマンはGoogle自動翻訳に頼りすぎも良くないのかもしれないというわけで、日本語記事が一つもなかったので誰かが今後引っかかったら屍を超えていってくれ

エラー対応 参考:

(ホントはあと10記事以上探していた)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?