Unexpected string in JSON at position 25 에러가 뜹니다.

23.09.18 18:14 작성 조회수 253

1

[인프런x코드캠프] 부트캠프에서 만든 고농축 백엔드 코스

에서 섹션22에 03-03 수업중

// const express = require("express"); // 옛날방식 => commonjs
import express from 'express'; // 요즘방식 => module

const app = express();
app.use(express.json());// 옛날에는 bodyParser 사용
app.get('/boards', function (req, res) {
  // 1. DB에 접속후, 데이터를 조회 => 데이터를 조회했다고 가정
  const result = [
    {
      number: 1,
      writer: "철수",
      title: "철수입니다~~",
      contents: "내용이에요!!!",
    },
    {
      number: 2,
      writer: "영희",
      title: "영희입니다~~",
      contents: "영희내용이에요!!!",
    },
    {
      number: 3,
      writer: "훈이",
      title: "훈이입니다~~",
      contents: "훈이내용이에요!!!",
    },
  ];
  // 2. DB에서 꺼내온 결과를 브라우저에 응답(response) 으로 주기
  res.send(result);
});

app.post('/boards', function (req, res) {
  // 1. 브라우저에서 보내준 데이터 확인하기
  console.log(req);
  console.log("===============================");
  console.log(req.body);

  // 2. DB에 접속 후, 데이터를 저장  => 데이터를 저장했다고 가정

  // 3. DB에 저장된 결과를 브라우저에 응답(response) 주기
  res.send("게시물 등록에 성공하였습니다.");
});

app.listen(3000);

여기서 app.use(express.json()) 을 추가한후 postmasn에서 post 했을때

 

스크린샷 2023-09-18 오후 6.08.04.png

SyntaxError: Unexpected string in JSON at position 25 에러를 띄웁니다.

 

다 잘 따라 한것 같은데 왜이런걸까요

 

이건 터미널 에러 전문입니다.

스크린샷 2023-09-18 오후 6.11.56.png

답변 1

답변을 작성해보세요.

0

미친 ... 포스트 하는 코드에 콤마를 빼먹었네요.... 어휴... 죄송합니다....