inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[리뉴얼] Node.js 교과서 - 기본부터 프로젝트 실습까지

Node.js 교과서

399

작성자 없음

작성한 질문수 0

0

안녕하세요 제로초님 Node.js 교과서를 사서 열심히 수업을 듣고 있는 한 학생입니다!

다름이 아니고 유튜브를보며 노드교과서 개정판 4-5. 쿠키 이해하기에서

코드 질문이 있어서 여쭤보러 왔습니다.

이것은 되는 코드이고

const parseCookies = (cookie = ''=>
    cookie
        .split(';')
        .map(v => v.split('='))
        .reduce((acc, [k,v]) => {
            acc[k.trim()] = decodeURIComponent(v)
            return acc;
       }, {});

이것은 안되는 코드입니다.

const parseCookies = (cookie = ''=> {
    cookie
        .split(';')
        .map(v => v.split('='))
        .reduce((acc, [k,v]) => {
            acc[k.trim()] = decodeURIComponent(v)
            return acc;
       }, {});
};

둘의 차이점은 함수를 {} 로 감싸냐 안감싸냐의 차이인데 화살표함수에서 중괄호는 선택사항이라고 배웠습니다.

만약 아래 코드를 사용하게 되면

(node:9452) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined

    at Server.<anonymous> (C:\Users\ksy\Desktop\Node.js\ch04\ex03\cookie2.js:30:22)

    at Server.emit (events.js:315:20)

    at parserOnIncoming (_http_server.js:874:12)

    at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)

(Use `node --trace-warnings ...` to show where the warning was created)

(node:9452) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

(node:9452) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

라는 에러가 뜹니다.  함수를 제대로 사용하지 못해서 그런거 같은데 왜 중괄호를 붙이면 함수가 실행이 안되는건지 궁금합니다

mongodb nodejs mysql Sequelize

답변 4

0

Han N

아 이해했습니다. node.js를 처음배우면서 알기 쉽게 가르쳐주셔서 잘 배우고있습니다.

감사합니다!!

0

제로초(조현영)

const a = () => {
  return 'b';
}

하는 것을 줄이는 게

const a = () => 'b';

입니다.

const a = () => {
  return { c: 'd' }
}

const a = () => ({ c: 'd' })

이고

const a = () => 
  e.f().g();

const a = () => {
  return e.f().g();
}

고요.

0

Han N

그럼 return문이 있으면 몇줄이 있던 중괄호를 쓰고

return문이 없으면 몇줄이 있더라도 중괄호를 쓰면 안되는건가요?

중괄호는 함수를 묶어주는 역할로 알고 있었는데 이것도 맞는지 궁금합니다!

0

제로초(조현영)

아뇨 화살표함수에서 중괄호는 선택사항이 아니라 문법에 매우 큰 영향을 미칩니다. 중괄호가 없으면 return문이고, 중괄호가 있으면 return 하지 않습니다.

리눅스 노드 설치시 패키지

0

159

0

socket.js 에서 referer로부터 roomId를 가져올 때

0

905

3

스트리밍 방식으로 대용량 파일 업로드 & 다운로드 관련 질문

0

2281

2

a[title] 질문드립니다

0

375

1

리뉴얼 강의 및 공부 방법

0

581

1

jwt decode

0

1128

1

node.js 교과서 3판 질문드립니다

0

391

1

passport와 jwt

0

432

1

리뉴얼 강의

0

418

2

혹시 Node.js 교과서 3판 이북은 언제 나오나요?

0

353

1

몽고디비 사용자도 MYSQL부분을 들어야 하나요???

0

481

1

sql 쿼리 로그는 어떤 모듈이 작성하나요?

0

505

2

nunjucks res.render('error'); 작동을 안합니다.

0

511

1

질문있습니다.

0

346

1

multer 한글 파일 업로드시 파일명이 깨져요.

1

3535

1

수업자료는 어디있나요?

0

366

1

질문 있습니디

0

235

1

multer 사용시 file 외 name값은 못받나용?

0

416

1

코드 중복 부분 질문드립니다.

0

298

1

api 만드는 이유 질문드립니다.

0

286

1

Strategy의 done에 대해 질문드립니다.

0

364

1

안녕하세요 fs 권한 관련 질문드립니다

0

448

1

시퀄라이즈 연결질문...

0

495

2

res.setHeader vs res.cookie

0

1786

3