[πŸ”₯ Firebase] Cloud Firestoreμ—μ„œ ν•˜μœ„ μ»¬λ ‰μ…˜ μΆ”κ°€ν•˜λŠ” 법 & μ½μ–΄μ˜€λŠ” 법

지은·2023λ…„ 3μ›” 28일
14

🌴 λͺ¨λ™μˆ² λ§ˆμΌ“

λͺ©λ‘ 보기
2/9
post-custom-banner

κ²Œμ‹œνŒ κΈ°λŠ₯을 κ΅¬ν˜„ν•˜λ˜ 쀑...
λ‚˜λŠ” κ²Œμ‹œκΈ€λ“€μ„ BoardsλΌλŠ” μ»¬λ ‰μ…˜μ— μ €μž₯ν–ˆκ³ , μ»¬λ ‰μ…˜ μ•ˆμ— μžˆλŠ” λ„νλ©˜νŠΈλ“€μ€ μ•„λž˜μ²˜λŸΌ 생겼닀.
(μ»¬λ ‰μ…˜μ€ 폴더 같은 κ°œλ…μ΄κ³ , λ„νλ©˜νŠΈλŠ” 폴더 μ•ˆμ— μžˆλŠ” 데이터라고 λ³΄μ‹œλ©΄ λ©λ‹ˆλ‹€!)

type: "buy"
title: "제λͺ©μž…λ‹ˆλ‹€."
body : "λ³Έλ¬Έμž…λ‹ˆλ‹€."
cart : [μ•„μ΄ν…œ1, μ•„μ΄ν…œ2, ...]
createdAt : 1679991242163
creatorDisplayName: "지은"
creatorId: "WOhBdlm4TZdXc5SdV51xcSY62"
done: false

이제 μ—¬κΈ°μ„œ κ²Œμ‹œκΈ€λ§ˆλ‹€ λŒ“κΈ€λ“€μ„ μ €μž₯ν•΄μ•Όν•˜λŠ”λ°, cart처럼 λŒ“κΈ€λ„ κ·Έλƒ₯ λ°°μ—΄λ‘œ 관리해도 λ˜μ§€ μ•Šλ‚˜..? μ‹Άμ—ˆμ§€λ§Œ λ‚˜μ€‘μ— κ²Œμ‹œκΈ€μ„ μˆ˜μ •(update)ν•  수 μžˆλŠ” κΆŒν•œμ„ κ²Œμ‹œκΈ€ μž‘μ„±μžλ§Œ κ°€λŠ₯ν•˜λ„λ‘ λ³΄μ•ˆ κ·œμΉ™μ„ μž‘μ„±ν•  κ²ƒμ΄μ˜€κΈ° λ•Œλ¬Έμ—, λŒ“κΈ€μ€ ν•˜μœ„ μ»¬λ ‰μ…˜μ„ λ§Œλ“€μ–΄ λ”°λ‘œ κ΄€λ¦¬ν•˜κΈ°λ‘œ ν–ˆλ‹€.

그런데 κ³΅μ‹λ¬Έμ„œμ— ν•˜μœ„ μ»¬λ ‰μ…˜μ˜ μ‘΄μž¬μ— λŒ€ν•œ μ„€λͺ…은 μžˆμ—ˆμ§€λ§Œ ν•˜μœ„ μ»¬λ ‰μ…˜μ„ μΆ”κ°€ν•˜κ³  μ½μ–΄μ˜€λŠ” 법은 아무리 뒀져봐도 μ„€λͺ…μ΄λ‚˜ μ˜ˆμ‹œ μ½”λ“œκ°€ μ—†μ—ˆλ‹€..
ν•œμ°Έ κ΅¬κΈ€λ§ν•˜λ‹€κ°€ μ°Ύμ•„λƒˆκ³ ..! λΈ”λ‘œκ·Έμ— 정리해두렀고 ν•œλ‹€ πŸ•Ί

ν•˜μœ„ μ»¬λ ‰μ…˜ μΆ”κ°€ν•˜λŠ” 법

import { db } from '../../fbase';
import { doc, setDoc, collection } from 'firebase/firestore';

...

const docRef = doc(collection(db, [μ»¬λ ‰μ…˜λͺ…], [λ„νλ©˜νŠΈλͺ…], [ν•˜μœ„ μ»¬λ ‰μ…˜λͺ…]));
await setDoc(docRef, [μ €μž₯ν•  데이터]);

μ˜ˆμ‹œ

try {
  const commentRef = doc(collection(db, 'Boards', id, 'Comments'));
  await setDoc(commentRef, requestData);
  alert('μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€.');
} catch (error) {
  console.log(error);
} finally {
  setCommentInput('');
  getComments();
}

μ΄λ ‡κ²Œ μž‘μ„±ν•˜λ©΄ BoardsλΌλŠ” μ»¬λ ‰μ…˜ μ•ˆμ— μžˆλŠ” id값에 ν•΄λ‹Ήν•˜λŠ” λ¬Έμ„œμ— CommentsλΌλŠ” ν•˜μœ„ μ»¬λ ‰μ…˜μ΄ μΆ”κ°€λ˜κ³ , ν•˜μœ„ μ»¬λ ‰μ…˜ μ•ˆμ—λŠ” λžœλ€ν•œ id둜 λ„νλ©˜νŠΈκ°€ μƒμ„±λ˜μ–΄ requestDataκ°€ λ‹΄κΈ΄λ‹€.


πŸ”Ό CommentsλΌλŠ” μ΄λ¦„μœΌλ‘œ ν•˜μœ„ μ»¬λ ‰μ…˜μ΄ μƒμ„±λœ 것을 확인할 수 μžˆλ‹€.

πŸ”Ό Comments μ»¬λ ‰μ…˜ μ•ˆμ— λ¬Έμ„œ(document)κ°€ λžœλ€ν•œ μ΄λ¦„μœΌλ‘œ μƒμ„±λ˜μ—ˆλ‹€. documentμ—λŠ” λ‚΄κ°€ requestData둜 μ „μ†‘ν•œ 값이 κ·ΈλŒ€λ‘œ λ‹΄κ²¨μžˆλ‹€.


ν•˜μœ„ μ»¬λ ‰μ…˜μ˜ λ„νλ©˜νŠΈ μ½μ–΄μ˜€λŠ” 법

import { db } from '../fbase';
import { collection, getDocs, query, orderBy } from 'firebase/firestore';

...

const q = query(collection(db, [μ»¬λ ‰μ…˜λͺ…], [λ„νλ©˜νŠΈλͺ…], [ν•˜μœ„ μ»¬λ ‰μ…˜λͺ…]));
// const q = query(collection(db, `μ»¬λ ‰μ…˜λͺ…/λ„νλ©˜νŠΈλͺ…/ν•˜μœ„ μ»¬λ ‰μ…˜λͺ…`)); 이 방법도 κ°€λŠ₯ν•˜λ‹€.
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  const docObj = {
    ...doc.data(),
    id: doc.id,
};

μ˜ˆμ‹œ

const getComments = async () => {
  const q = query(collection(db, 'Boards', id, 'Comments'), orderBy('createdAt', 'desc'));
  // const q = query(collection(db, `Boards/${id}/Comments`), orderBy('createdAt', 'desc'));
  const querySnapshot = await getDocs(q);
  const querySnapshot = await getDocs(q);
  querySnapshot.forEach((doc) => {
  const docObj = {
      ...doc.data(),
      commentId: doc.id,
    };
  setComments((comments) => [...comments, docObj]);
  });
};


πŸ”Ό μ΄λ ‡κ²Œ Comments μ»¬λ ‰μ…˜μ— λ‹΄κΈ΄ λ„νλ©˜νŠΈλ“€μ΄ 잘 λ°›μ•„μ™€μ§€λŠ” 것을 확인할 수 μžˆλ‹€.
λ‚˜λŠ” λŒ“κΈ€μ„ μ΅œμ‹ μˆœμœΌλ‘œ μ •λ ¬ν•˜κΈ° μœ„ν•΄ orderBy()도 μΆ”κ°€ν•˜μ—¬ μ‚¬μš©ν–ˆλ‹€.


ν•˜μœ„ μ»¬λ ‰μ…˜μ˜ λ„νλ©˜νŠΈ μ‚­μ œν•˜λŠ” 법

import { doc, deleteDoc }  from 'firebase/firestore';

...

const desertRef = doc(db, [μ»¬λ ‰μ…˜λͺ…], [λ„νλ©˜νŠΈλͺ…], [ν•˜μœ„μ»¬λ ‰μ…˜λͺ…], [μ‚­μ œν•  λ„νλ©˜νŠΈλͺ…]);
await deleteDoc(desertRef);

μ˜ˆμ‹œ

const deleteComment = async (commentId: string) => {
  const confirm = window.confirm('μ •λ§λ‘œ μ‚­μ œν•˜κ² μŠ΅λ‹ˆκΉŒ?');

  if (confirm) {
    const desertRef = doc(db, 'Boards', id, 'Comments', commentId);
    try {
      await deleteDoc(desertRef);
    } catch (error) {
      console.log(error);
    }
  }
};

μ΄λ ‡κ²Œ μž‘μ„±ν•˜λ©΄ κ²Œμ‹œνŒ μ»¬λ ‰μ…˜(Boards) μ•ˆμ˜ ➑️ κ²Œμ‹œκΈ€ λ„νλ©˜νŠΈ(id) μ•ˆμ˜ ➑️ ν•˜μœ„ μ»¬λ ‰μ…˜(Comments) μ•ˆμ˜ ➑️ commentIdλ₯Ό μ΄λ¦„μœΌλ‘œ 가진 λŒ“κΈ€ λ„νλ©˜νŠΈλ₯Ό μ‚­μ œν•  수 μžˆλ‹€.


쨘 🌟

이 글은 μ•„λž˜ 링크λ₯Ό μ°Έκ³ ν•˜μ—¬ μž‘μ„±ν•œ κΈ€μž…λ‹ˆλ‹€.
How to Add a Subcollection to a Document in Firebase Cloud Firestore?
FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but services/p0dnjzr7HZrcp6Mifiqh has 2

profile
λΈ”λ‘œκ·Έ 이전 -> https://janechun.tistory.com
post-custom-banner

7개의 λŒ“κΈ€

comment-user-thumbnail
2023λ…„ 3μ›” 29일

μš°μ™€ μ±„νŒ…... λ„ˆλ¬΄ λ©‹μ§‘λ‹ˆλ‹€ νŒŒμ΄μ–΄λ² μ΄μŠ€ μ‚¬μš©ν• λ•Œ μ°Έκ³  ν•˜λ©΄ 쒋을것 κ°™μ•„μš” !

λ‹΅κΈ€ 달기
comment-user-thumbnail
2023λ…„ 3μ›” 29일

우였였 μ°Έκ³ ν•œ 글듀이 보기만 해도 머리가 ν•‘ν•‘ λ„λ„€μš” γ…‹γ…‹γ… 

λ‹΅κΈ€ 달기

와... λ„ˆλ¬΄ λ©‹μ§„λ°μš” ??

λ‹΅κΈ€ 달기
comment-user-thumbnail
2023λ…„ 4μ›” 2일

였.. νŒŒμ΄μ–΄λ² μ΄μŠ€ μ‚¬μš© μ€‘μ΄μ‹ κ°€μš”? Vercel, Netlify만 μΌμ—ˆλŠ”λ° 이것도 λ‚˜μ€‘μ— μ“Έ 생각 μžˆμ—ˆλŠ”λ° 잘 λ³΄κ² μ”λ‹ˆλ‹€

λ‹΅κΈ€ 달기
comment-user-thumbnail
2023λ…„ 4μ›” 2일

μš°μ™€ μ™„μ „ λ©‹μžˆμλ‹ˆλ‹Ή

λ‹΅κΈ€ 달기
comment-user-thumbnail
2023λ…„ 4μ›” 2일

κ°“ νŒŒμ΄μ–΄λ² μ΄μŠ€. κ·Έκ±Έ 자유자재둜 μ‚¬μš©ν•˜μ‹œλŠ” κ°“μ§€μ€λ‹˜

λ‹΅κΈ€ 달기
comment-user-thumbnail
2024λ…„ 5μ›” 13일

잘 μ½κ³ κ°‘λ‹ˆλ‹€ !!

λ‹΅κΈ€ 달기