ZeroCho / nodejs-book

Node.js교과서 소스 코드

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[p.399] Mongoose 연결 함수 호출 시 콜백 매개변수 관련 에러

wiseguy77 opened this issue · comments

commented

[환경]
M1 chip, macOS Ventura
mongodb/brew/mongodb-community: stable 6.0.6
mongoose 7.3.1
node 18.15.0

[에러]
MongooseError: Mongoose.prototype.connect() no longer accepts a callback
at Mongoose.connect (/Users/wise/study/nodejs/nodejs-textbook/learn-mongoose/node_modules/mongoose/lib/index.js:400:11)
at connect (/Users/wise/study/nodejs/nodejs-textbook/learn-mongoose/schemas/index.js:7:12)
at Object. (/Users/wise/study/nodejs/nodejs-textbook/learn-mongoose/app.js:15:1)

[해결]

  1. schema/index.js 파일의 connect 함수의 콜백 부분 주석 처리 후 ,
    const connect = () => {
    if (process.env.NODE_ENV !== "production") {
    mongoose.set("debug", true);
    }
    mongoose.connect(
    "mongodb://wise:1234@localhost:27017/admin",
    {
    dbName: "nodejs",
    useNewUrlParser: true,
    },
    // (error) => {
    // if (error) {
    // console.log("몽고디비 연결 에러", error);
    // } else {
    // console.log("몽고디비 연결 성공");
    // }
    // }
    );
    };

  2. connected 이벤트 핸들러 등록으로 대체
    mongoose.connection.on('connected', () => {
    console.log("몽고디비 연결 성공");
    });

2쇄 책에서는
connect에 콜백함수 대신 then, catch를 붙이는 것으로 수정되었습니다. 감사합니다.