• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

Node.js 교과서

21.01.19 19:08 작성 조회수 210

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.

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

답변 4

·

답변을 작성해보세요.

0

Han N님의 프로필

Han N

2021.01.19

아 이해했습니다. 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님의 프로필

Han N

2021.01.19

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

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

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

0

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