E11000 duplicate key error collection

Jean·2023년 6월 14일
0

데이터 넣디가 오류가 났다...

MongoServerError: E11000 duplicate key error collection: spa_mall.comments index: postId_1 dup key: { postId: 2 }
    at /Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/operations/insert.js:50:33
    at /Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/cmap/connection_pool.js:331:21
    at /Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/sdam/server.js:207:17
    at handleOperationResult (/Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/sdam/server.js:323:20)
    at Connection.onMessage (/Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/cmap/connection.js:206:9)
    at MessageStream.<anonymous> (/Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/cmap/connection.js:61:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/cmap/message_stream.js:124:16)
    at MessageStream._write (/Users/jeanmiles/Desktop/sparta/spa_mall/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12) {
  index: 0,
  code: 11000,
  keyPattern: { postId: 1 },
  keyValue: { postId: 2 },
  [Symbol(errorLabels)]: Set(0) {}
}

찾아보니 인덱스?가 겹치면 생기는 문제 같아서
유니크 값을 바꾸어 주었다...

const commentSchema = new mongoose.Schema({
  postId: {
    type: Number,
    required: true,
  },
  content: {
    type: String,
    required: true,
  },
  commentId: {
    type: Number,
    required: true,
    unique: true,
  },
  date: {
    type: Date,
    default: new Date(new Date().getTime() + 1000 * 60 * 60 * 9),
  },
});

하지만 어림도 없지!!!
그래도 문제는 지속되고..

깔끔하게 콜렉션을 버렸다ㅋㅋㅋㅋㅋ.......ㅠㅠㅜㅜ

버리니 정상 작동을했다..... 이게 맞냐..

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 오류

해당오류는 response를 중복되게 날렸을때 생깁니다...

//댓글 생성
router.post("/comment", async (req, res) => {
  const { postId, content, commentId } = req.body;
  //댓글 내용 입력했는지
  if (!content.length) {
   res
      .status(400)
      .json({ success: false, msg: "댓글 내용을 입력해주세요" });
  }
  //게시글 아이디가 존재하는지
  const postIdFind = await Post.find({ postId });
  if (!postIdFind.length)
    res
      .status(400)
      .json({ success: false, msg: "해당 게시글이 존재하지 않습니다" });
  //댓글 아이디 중복
  const commentIdFind = await Comment.find({ commentId });
  if (!commentIdFind.length)
    res
      .status(400)
      .json({ success: false, msg: "해당 댓글아이디는 이미 존재합니다" });
  await Comment.create({
    postId,
    content,
    commentId,
  });

  res.status(200).json({ success: true, msg: "댓글이 등록되었습니다." });
});

혹시 당신if문에 return을 안 붙이셨나요?
return을 넣으면 잘 돌아갑니다....후...

return을 안 넣으면 다음줄로 넘어가기 때문에 res가 여러번 보내지고
해당 오류가 발생하는듯..?


//댓글 생성
router.post("/comment", async (req, res) => {
  const { postId, content, commentId } = req.body;
  //댓글 내용 입력했는지
  if (!content.length) {
    return res
      .status(400)
      .json({ success: false, msg: "댓글 내용을 입력해주세요" });
  }
  //게시글 아이디가 존재하는지
  const postIdFind = await Post.find({ postId });
  if (!postIdFind.length)
    return res
      .status(400)
      .json({ success: false, msg: "해당 게시글이 존재하지 않습니다" });
  //댓글 아이디 중복
  const commentIdFind = await Comment.find({ commentId });
  if (!commentIdFind.length)
    return res
      .status(400)
      .json({ success: false, msg: "해당 댓글아이디는 이미 존재합니다" });
  await Comment.create({
    postId,
    content,
    commentId,
  });

  res.status(200).json({ success: true, msg: "댓글이 등록되었습니다." });
});

return 을 생활화 합시다...

profile
햇내기 개발자 지망생

0개의 댓글