-
카테고리
-
세부 분야
풀스택
-
해결 여부
미해결
=> 형식의 함수를 활용하면 에러가 뜹니다.
18.10.07 19:38 작성 조회수 119
0
comments.js 에서 똑같이 해봤는데, 동영상에서 보여주신 대로 delete 부분에서 => 형식의 콜백을 썼더니 에러가 납니다.
반면, github에 올리신 파일에는 function(){} 형식의 콜백이더군요. 실제로 function(){}으로 바꾸니 에러가 없이 잘 되기 시작했습니다.
어쩨서 강좌 중에서는 =>의 화살표 함수를 써도 이상이 없는 건가요? ㅜ.ㅜ
제가 이해한 바로는 이 경우에는 오히려 스스로의 this를 가지지 않는 => 형식의 함수를 써야 하는 거 같은데요... ㅜ.ㅜ
이하는 코드 전문입니다. 코드의 router.delete('/:id', function(req, res, next) 부분을 function()이 아닌 => 형식으로 바꾸면 에러가 터집니다.
const express = require('express');
const router = express.Router(); //괄호가 있으면 실행된 값을 배정.
const {User, Comment} = require('../models');
//GET /comment
//get /comment/id 와는 같지 않ㅌ다.
router.get('/:id', (req, res, next)=> {
Comment.findAll({
include:{
model: User,
where: {id: req.params.id},
}
})
.then((comments)=>{
console.log(comments);
res.json(comments)
})
.catch((err)=>{
console.error(err);
next(err);
})
})
router.patch('/:id', (req, res, next) =>{
Comment.update({
comment: req.body.comment,
}, {
where:{id: req.params.id }
})
.then((result)=>{
console.log(result);
res.json(result);
})
.catch((err)=>{
console.error(err);
next(err)
})
})
router.delete('/:id', function(req, res, next) {
Comment.destroy({ where: { id: req.params.id } })
.then((result) => {
res.json(result);
})
.catch((err) => {
console.error(err);
next(err);
});
});
router.post('/', function (req, res, next){
Comment.create({
commenter: req.body.id,
comment: req.body.comment,
})
.then((result)=>{
console.log(result);
res.json(result);
})
.catch((err)=>{
console.error(err);
next(err)
})
}) // post에는 :id가 필요 억는 것에 주목. 생성한 후에, id가 생성 되는 거기 때문.
module.exports = router;
답변을 작성해보세요.
0
0
답변 2