[express] mongoose [MongooseError: Model.prototype.save() no longer accepts a callback] 에러 + mongoose 버전 다운그레이드
<문제 상황>
JSON 형태로 보내는 과정에서
말 그대로 MongooseError: Model.prototype.save() no longer accepts a callback 에러가 떴었다.
이유를 찾아보니까, mongoose 7.0(?) 아무튼 현재 버전에서는 문법이 바꼈단다.
콜백 함수를 더이상 지원하지 않는다고.. 어떡하지? ㅎㅎ
Mongoose stopped accepting callbacks for some of its functions
I've been using callbacks for .save() and .findOne() for a few days now and just today I encounter these errors: throw new MongooseError('Model.prototype.save() no longer accepts a callback')
stackoverflow.com
< 해결 방법 >
1. 코드를 뜯어 고친다. -> 가장 좋은 방법이겠지만, 강의를 듣고 있는 입장이라 어려울 것으로 판단됨.
2. mongoose 다운 그레이드. 급한대로 mongoose를 다운 그레이드 하기로 했다.
강의에 나오는 문법이 적용되지 않습니다 - 인프런 | 질문 & 답변
MongooseError: Model.prototype.save() no longer accepts a callback이 오류가 떠서 확인해보니 Mongoose6부터 callback 문법이 사용되지 않는다고 합니다버전을 바꾸는 것은 시도 하지 않았고.then .catch ...
www.inflearn.com
여기를 참조하여 package.json 파일에서, monoose 의 버전을 5로 낮추고,
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
"mongoose": "^5.13.17"
}
}
$ npm install mongoose --save
설치를 완료하였다.
그리고 connect 부분에 밑의 코드를 추가해준다.
mongoose.connect(config.mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,})
.then(() => console.log("MongoDB connected"))
.catch((err) => console.log(err));
맞는 방법인지는 모르겠지만 일단 돌아간다! 휴!