먼저 아래 명령어를 통해 mongodb가 잘 설치되었는지 확인한다.
mongod
mongo
에러:
zsh: command not found: mongo
아래 블로그를 통해 mongodb의 쉘을 설치하여 해결했다.
https://velog.io/@jiminnote/zsh-command-not-found-mongo
zsh: command not found: mongo
=>몽고DB 설치하다가 생긴 장애물몽고DB 설치해결법 1.몽고디비 재실행 - 실패✅ 해결법 2. 성공
velog.io
그리고 현재 작업 중인 src 디렉토리에 db.js 파일을 만들고 아래 코드를 작성했다.
import mongoose from "mongoose";
mongoose.set('strictQuery', false);
mongoose.connect("mongodb://127.0.0.1:27017/wetube");
const db = mongoose.connection;
const handleError = (error) => {
console.log("❗️DB error: ", error);
}
const handleOpen = () => {
console.log("✅DB is connected");
}
db.on("error", handleError);
db.once("open", handleOpen);
처음에 connect하니 mongoose로부터 이런 warning을 받았다.
(node:20648) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
그래서 조언대로 아래 코드를 작성했다. (connect보다 먼저 작성해야 한다)
mongoose.set('strictQuery', false);
그리고 db의 상태에 따라 연결됨과 에러를 표시하는 controller를 설정했다.
const handleError = (error) => {
console.log("❗️DB error: ", error);
}
const handleOpen = () => {
console.log("✅DB is connected");
}
db.on("error", handleError);
db.once("open", handleOpen);
on은 이벤트처럼 여러 번 error가 일어날 때마다 실행되고, once는 open될 때 한 번 실행된다.
잘 작동한다.
'프로젝트 > 유튜브 클론코딩' 카테고리의 다른 글
[노마드스터디] 주간회고록 9주차 (0) | 2023.03.02 |
---|---|
user authentication 1: hash (0) | 2023.02.20 |
mongodb and mongoose 6: pre, middleware, static (0) | 2023.02.17 |
[노마드스터디] 주간회고록 7주차 (0) | 2023.02.16 |
[노마드스터디] 주간회고록 5주차 (0) | 2023.02.02 |
댓글