=> 형식의 함수를 활용하면 에러가 뜹니다.
205
작성한 질문수 28
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;
답변 2
실제 서비스에서도 cluster를 사용하나요?
1
314
2
캐싱에 관하여
0
279
3
salt를 실무에서 사용할때 항상 randomBytes로 만들어줘야 하나요?
0
347
1
게시물 올리기 오류
0
408
1
캐슁 이후 로그인창
0
266
1
kakao passport 질문있습니다.
0
493
3
global객체 공유 질문드립니다.
1
418
1
서버가 죽어버리네요
0
1047
8
포링키 문제..
0
235
2
커넥션 플래시 설치문제
0
216
2
익스프레스 제너레이터? 설치문제
0
2586
6
redis 질문입니다.
0
305
1
9장 세션을 DB에 저장시 리다이렉션 오류 발생
0
874
8
프레임워크 선택에 관하여 질문이 있습니다.
0
300
3
시퀄라이즈 질문입니다 ! !
0
588
1
리뉴얼 강의 12강 socket.io에서 req.session접근 관련 질문
0
922
6
oAuth 질문입니다.
0
356
3
GCP 질문입니다.
0
366
1
Passport 모듈 로그인 구현관련
0
720
6
카카오 로그인 관련 질문입니다!!
0
606
4
스스로 해보기 10-16 nunjuncks 질문있습니다
0
499
5
제로초님 HTTP 완벽가이드 추천해주신거 너무 잘 읽었습니다.
0
317
2
gif채팅방 nunjucks관련 질문입니다!
0
338
3
현영님 몽고db관련 질문입니다.
0
233
3





