import { db } from "@/api/firebase";
import { useRouter } from "next/router";
...
const router = useRouter();
console.log(db);
const data = doc(db, "board", toString(router.query.router));
console.log(data.docs);
처음엔 toString을 하지 않아서 number로 들어가서 type error가 났었다.
data를 찍어보면 이렇게 나온다.
뿅하고 바로 의미있는 데이터가 보일 줄 알았는데
내가 뭘 잘못한건지 아니면 아예 찾아올 게 없어서 그러는지 . . . .
useEffect(() => {
const docRef = doc(db, "board", toString(router.query.router));
const getSnap = async () => {
const docSnap = await getDoc(docRef);
console.log(docSnap.data());
};
getSnap();
}, []);
이렇게 했지만 undefined가 뜨네...
그것은 이렇게 썼기 때문이다.
FirebaseError: Expected type 'Ja', but it was: a custom Za object
-doc을 docs로 썼거나 docs를 doc으로 썼거나.
useEffect(() => {
const docRef = collection(db, "board");
const q = query(docRef, where("createdAt", "==", String(router.query._id)));
console.log("q:", q);
const getSnap = async () => {
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log("dddd");
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
};
getSnap();
}, []);
공식 문서를 보고 거의 똑같이 따라했는데, forEach안의 "dddd"는 오지도 않는다.
아무것도 없는건가?
그렇다 아무것도 없는 것이다.
createdAt에 머슨 문제가 있나.
title을 얍으로 하고 해봤는데 됨!
와 문제는 이거였다.
이게 undefined로 뜬다.
typeof로 확인해보니 router.query._id는 이미 string이었다.
이번엔 이게 떴다.
혹시몰라 변수에 소중하게 담아봤는데 그건 소용 없었다.
이렇게 쓰면 아무 데이터도 안 온다.
그것은....
router.query._id를 Number로 감싸주니 해결되었다...
생성할 때 number로 들어가는듯
https://firebase.google.com/docs/firestore/query-data/get-data?hl=ko
Cloud Firestore로 데이터 가져오기 | Firebase
5월 10일, Google I/O에서 Firebase가 돌아옵니다. 지금 등록하기 의견 보내기 Cloud Firestore로 데이터 가져오기 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 세 가
firebase.google.com
'프론트엔드✏️ > 개인공부' 카테고리의 다른 글
[git] (0) | 2023.04.06 |
---|---|
[git] To push the current branch and set the remote as upstream, use ... (0) | 2023.04.06 |
[tailwind] tailwindcss 색상, 배경색 적용 안될 때 (0) | 2023.03.23 |
[react] 계산기/ 무한 리렌더링, 맨 왼쪽의 0 좀 빼줘 (0) | 2023.03.06 |
[react] 계산기/input 두 개가 서로 영향받게 하기 (0) | 2023.03.03 |