ZeroCho / nodejs-book

Node.js교과서 소스 코드
793 stars 636 forks source link

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

Open wiseguy77 opened 1 year ago

wiseguy77 commented 1 year ago

[환경] 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("몽고디비 연결 성공"); });

ZeroCho commented 1 year ago

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