TIL 25일차: 노드 입문 과제 오류 수정 중
1. 로컬에서는 잘 되는데 웹서버에서는 안되는 것들이 있다.
예컨대 댓글 목록 조회...
router.get("/comments/:_id", async(req,res) => {
const {_id} = req.params
const comments = await Comment.find({postsId: _id})
res.json({comments})
})
여기에서 세번째 줄에postsId는 스키마 값에 있는 키값인데,
내가 시도해본 바에 따르면 postsId값에 _id값이 들어가는 걸로 이해했는데... 이게 맞나.
이럼 로컬에서는 잘 되는데 서버에서는 안된다.
오류 메시지는 다음과 같다.
if (fields[key].$elemMatch)
TypeError: Cannot read properties of undefined (reading '$elemMatch') at model.Query._castFields (/home/ubuntu/nodejs_beginner_assignment/node_modules/mongoose/lib/query.js:5362:23) at model.Query.<anonymous> (/home/ubuntu/nodejs_beginner_assignment/node_modules/mongoose/lib/query.js:2235:23) at model.Query._wrappedThunk [as _find] (/home/ubuntu/nodejs_beginner_assignment/node_modules/mongoose/lib/helpers/query/wrapThunk.js:29:8) at /home/ubuntu/nodejs_beginner_assignment/node_modules/kareem/index.js:494:25
얘를 어떻게 고쳐줘야할지...
처음에는 array find 쓰는 순서를 잘 몰라서 그런거라고 생각해서 찾아봤는데,
왼쪽이 찾는/의미 부여하는 값이고, 오른쪽이 value맞는데...
Array.prototype.find() - JavaScript | MDN
The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
developer.mozilla.org
오전에 튜터님이랑 이것저것 확인해보다가
결국 Studio 3T에서 원격으로 연결이 안되어서 콜렉션을 드롭시키지 못하므로
Comment를 Comment2라는 이름으로 바꾸어서 다시 저장하게 했다.
말 그대로 새 콜렉션을 파서 그쪽으로 연결하게 한 것.
그랬더니 원격에서도 댓글 get 기능이 구현된다.
제출까지 한 시간 ip주소로 들어갈 수 있는 건 결국 두개 뿐이다... ㅠㅠ
posts랑 comments/:_id
이렇게 했는데도 30분이 남으니 여유롭게 put이랑 delete도 공부하러 가볼까... 뚀륵