๐Ÿ“Œ [ERROR] Cannot read property 'uid' of undefined

Markยท2022๋…„ 3์›” 12์ผ
2
post-thumbnail

๐Ÿ“Œ ๋ฌธ์ œ ์›์ธ

uid๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ
req.body๋Š” body-parser๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋””ํดํŠธ ๊ฐ’์œผ๋กœ undefined๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ

โœ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

body์˜ json์„ ํŒŒ์‹ฑํ•ด์ฃผ๋Š” 'body-parser'๋ฅผ install ์‹œ์ผœ์คŒ

$ npm install body-parser
const bodyParser = require('body-parser');

router.use(
    bodyParser.urlencoded({
        extended: false,
    })
);
router.use(bodyParser.json());


router.post('/info', async (req, res, next) => {
    let userId = req.body.uid;
    let userEmail = req.body.email;
    User.create({
            uid: userId,
            email: userEmail,
        })
        .then((result) => {
            console.log(result);
            res.status(200).json(result);
        })
        .catch((err) => {
            console.log(req.body)
            console.error(err);
            next(err);
        })
})

Reference

https://medium.com/@chullino/1%EB%B6%84-%ED%8C%A8%ED%82%A4%EC%A7%80-%EC%86%8C%EA%B0%9C-body-parser%EB%A5%BC-%EC%86%8C%EA%B0%9C%ED%95%A9%EB%8B%88%EB%8B%A4-%ED%95%98%EC%A7%80%EB%A7%8C-body-parser%EB%A5%BC-%EC%93%B0%EC%A7%80-%EB%A7%88%EC%84%B8%EC%9A%94-bc3cbe0b2fd

https://www.npmjs.com/package/body-parser

profile
๊ฐœ์ธ ๊ณต๋ถ€ ์ •๋ฆฌ

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 4์›” 29์ผ

๋ง‘ ํ™”์ดํŒ…

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ