• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Node.js, Express 게시판 2-5 강의에서 질문

2-5 강의에서 질문

8 글 보임 - 1 에서 8 까지 (총 8 중에서)
  • 글쓴이
  • #47545

    코딩이
    참가자
    (node:61036) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: todoapp.post index: _id_ dup key: { _id: 1 }
        at Function.create (C:\todoapp\node_modules\mongodb\lib\core\error.js:57:12)
        at toError (C:\todoapp\node_modules\mongodb\lib\utils.js:123:22)
        at C:\todoapp\node_modules\mongodb\lib\operations\common_functions.js:258:39
        at handler (C:\todoapp\node_modules\mongodb\lib\core\topologies\replset.js:1210:22)
        at C:\todoapp\node_modules\mongodb\lib\core\connection\pool.js:405:18
        at processTicksAndRejections (internal/process/task_queues.js:79:11)
    (node:61036) 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:61036) [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.
    PS C:\todoapp> node server.js
    Debugger attached.
    (node:47044) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new
    Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
    listening on 8080
    저장완료
    11
    11
    0
    저장완료
    11
    11
    저장완료
    0
    (node:47044) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: todoapp.post index: _id_ dup key: { _id: 1 }
        at Function.create (C:\todoapp\node_modules\mongodb\lib\core\error.js:57:12)
        at toError (C:\todoapp\node_modules\mongodb\lib\utils.js:123:22)
        at C:\todoapp\node_modules\mongodb\lib\operations\common_functions.js:258:39
        at handler (C:\todoapp\node_modules\mongodb\lib\core\topologies\replset.js:1210:22)
        at C:\todoapp\node_modules\mongodb\lib\core\connection\pool.js:405:18
        at processTicksAndRejections (internal/process/task_queues.js:79:11)
    (node:47044) 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)
    
    
    
    이렇게 뜨면서 0은 출력이 잘되는데 1부터 출력이 안되네요
    조언을 구합니다
    (node:47044) [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.
    
    #47549

    코딩이
    참가자
    정확히 말씀드리면 0까지 나오고 그다음부터 에러가 뜨면서 서버가 다운됩니다
    #47585

    코딩이
    참가자
    여기 제 server.js 코드입니다
    
    
    const express = require("express");
    const app = express();
    const bodyParser = require("body-parser");
    app.use(bodyParser.urlencoded({ extended: true }));
    const MongoClient = require("mongodb").MongoClient;
    app.set("view engine", "ejs");
    var db;
    MongoClient.connect(
      "mongodb+srv://gihwan:@atlascluster.u961rpo.mongodb.net/?retryWrites=true&w=majority",
      function (err, client) {
        if (err) return console.log(err);
        db = client.db("todoapp");
        db.collection("post").insertOne(
          { 제목: "코딩애플", 날짜: "2022년 9월 22일" },
          function (err, res) {
            console.log("저장완료");
          }
        );
        app.listen(8080, function () {
          console.log("listening on 8080");
        });
      }
    );
    app.get("/pet", function (req, res) {
      res.send("반갑습니다");
    });
    app.get("/beauty", function (req, res) {
      res.send("고맙습니다");
    });
    app.get("/", function (req, res) {
      res.sendFile(__dirname + "/write.html");
    });
    app.get("/index", function (req, res) {
      res.sendFile(__dirname + "/index.html");
    });
    app.post("/add", function (req, res) {
      console.log(req.body.title);
      console.log(req.body.date);
      db.collection("counter").findOne(
        { name: "게시물갯수" },
        function (err, result) {
          console.log(result.totalPost);
          var 총게시물갯수 = result.totalPost;
          db.collection("post").insertOne({
            _id: 총게시물갯수 + 1,
            제목: req.body.title,
            날짜: req.body.date,
          });
        }
      );
      db.collection("post").insertOne(
        { 제목: req.body.title, 날짜: req.body.date },
        function (err, res) {
          console.log("저장완료");
        }
      );
      res.send("전송완료");
    });
    app.get("/list", function (요청, 응답) {
      db.collection("post")
        .find()
        .toArray(function (에러, 결과) {
          console.log(결과);
          응답.render("list.ejs", { posts: 결과 });
        });
    });
    감사합니다
    #47591

    codingapple
    키 마스터
    post컬렉션에 있는 게시물 다 지우고 다시 해봅시다
    #47659

    코딩이
    참가자
    한 가지만 더 질문드리겠습니다. 이거 원래 totalPost 갯수 증가하는 거 없으면 저장이 counter에서 1이상 안되는 게 맞나요>
    #47660

    코딩이
    참가자
    해결되었습니다 감사합니다 나중에 추가하니까 댓글이 더 달리네요
    #47661

    코딩이
    참가자
    질문이 있습니다 totalPost의 갯수가 데이터를 삭제한뒤에도 그대로 남아있는데 어떻게 설정하는지 강의에 나와있나요?
    #47674

    codingapple
    키 마스터
    글삭제시 직접 -1 하라고 코드짜면 됩니다 글번호 기록용이면 -1 안하는게 좋습니다
8 글 보임 - 1 에서 8 까지 (총 8 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관, 개인정보처리방침
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠