๐Ÿ”ฅFirestore ์‘์šฉ์œผ๋กœ ๋Œ“๊ธ€ ๋งŒ๋“ค๊ธฐ + ๋ฌธ์ œ ํ•ด๊ฒฐ (ํŒ€ํ”„๋กœ์ ํŠธ 3์ผ์ฐจ)

๋ฆผ๋ฏผ์ง€ยท2025๋…„ 2์›” 20์ผ

Today I Learn

๋ชฉ๋ก ๋ณด๊ธฐ
7/62

๋Œ“๊ธ€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค

<div class="comment">
	<div class="commentList">  
      <div class="speech-bubble">
        ์•ˆ๋…•ํ•˜์„ธ์š”
		</div>
	</div>
	<div class="commentMake">
  	<input type="email" class="commenting" id="commenting" placeholder="๋Œ“๊ธ€์„ ๋‹ฌ์•„์ฃผ์„ธ์š”" required>
    	<button id="submmit"><img src="/img/send.png"></button>
	</div>
</div>

commentList ๋‚ด๋ถ€์— speech-bubble ์ด๋ผ๋Š” ์ฑ„ํŒ… ๋ธ”๋Ÿญ์„ ๋งŒ๋“ค์–ด ๋„ฃ๊ณ  ์‹ถ์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  commentMake ์นธ์˜ Input์„ ์‚ฌ์šฉํ•ด ๋Œ“๊ธ€์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.

๐Ÿ’ซ JsํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

ํŒŒ์ผ์„ ๋‚˜๋ˆ ์„œ ๊ด€๋ฆฌํ•˜๋ฉด ๋ณด๊ธฐ๋„ ์‰ฝ๊ณ , ์ฝ”๋“œ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—

1. ๊ธฐ๋ณธ ์„ธํŒ…ํ•˜๊ธฐ

์šฐ์„  htmlํŒŒ์ผ์— js๋ฅผ ๋ถˆ๋Ÿฌ์™€์•ผ๊ฒ ์ง€์˜ค

์ œ์ด์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
js ์ฝ”๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ
<script type="module" src="memberIntro.js"></script>

๋‹ค์Œ์— JsํŒŒ์ผ์— Firestore ์„ธํŒ…์„ ํ•ด๋ณด์ž

// Firebase SDK ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-app.js";
import { getFirestore } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-firestore.js";
import {collection, addDoc} from "https://www.gstatic.com/firebasejs/9.22.0/firebase-firestore.js";
import { getDocs } from "https://www.gstatic.com/firebasejs/9.22.0/firebase-firestore.js";

// Firebase ๊ตฌ์„ฑ ์ •๋ณด ์„ค์ •
const firebaseConfig = {
// ์ž์‹ ์˜ firebase ์ •๋ณด ์ž…๋ ฅํ•˜๊ธฐ
};
// Firebase ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™”
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

2. addDoc

์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋ฉด ์ž๋™์œผ๋กœ ๋จผ์ € ์‹คํ–‰๋˜๋„๋กํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์“ฐ์ž

$(document).ready(async function () {
});

firestore์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง‘์–ด๋„ฃ์œผ๋ ค๋ฉด? โ†’ addDoc์„ ์“ฐ์ž!

$("#submmit").click(async function () {
    let comment = $("#commenting").val();

    if (comment === "") {
      alert("๋Œ“๊ธ€์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!");
      return;
    }
    addDoc(collection(db, "commenting"), { text: comment });

    try {
      await addDoc(collection(db, "commenting"), {
        text: comment,
        timestamp: new Date(),
      });
      console.log("๋Œ“๊ธ€ ๊ฒŒ์‹œ ์™„๋ฃŒ");
      $("#commenting").val(""); // ์ž…๋ ฅ์ฐฝ ๋น„์šฐ๊ธฐ
    } catch (error) {
      console.error("Error adding document: ", error);
      alert("๋Œ“๊ธ€ ์ €์žฅ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
    }
  });

commenting์ปฌ๋ž™์…˜์„ ๋งŒ๋“ค์–ด์„œ ์•ˆ์— ๋Œ“๊ธ€์„ ๋„ฃ์–ด๋ณด์ž
addDoc(collection(db, "commenting"), { text: comment });
db์— text๋กœ comment์— ์žˆ๋Š” ๋‚ด์šฉ์„ ๋„ฃ์ž

if (comment === "") {
    alert("๋Œ“๊ธ€์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!");
    return;
  }

๋งŒ์•ฝ ์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด, ์•Œ๋žŒ์„ ๋„์šด๋‹ค
$("#commenting").val(""); ๋ฅผ ํ™œ์šฉํ•ด์„œ ์ž…๋ ฅํ•˜๋ฉด ์ž…๋ ฅ์ฐฝ์„ ๋น„์›Œ์ฃผ์ž
๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ firestore์— ์ž˜ ์ €์žฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

3. getDocs

์ด์ œ๋Š” ์ €์žฅํ•œ๊ฑธ ๊ฐ€์ ธ์™€์•ผํ•œ๋‹ค.

  let docs = await getDocs(collection(db, "commenting"));
  //1
    docs.forEach((doc) => {
  //2
      let commentData = doc.data();
      let temp_html = `<div class="speech-bubble">${commentData.text}</div>`;
      $(".commentList").append(temp_html);
  
  });

db์— commenting์œผ๋กœ ์ €์žฅํ–ˆ์œผ๋‹ˆ ๋™์ผํ•˜๊ฒŒ ๊ฐ€์ ธ์™€์•ผ๊ฒ ์ฃ ????
๊ทธ๋ฆฌ๊ณ  commenting์ด๋ผ๋Š” ์ปฌ๋ž™์…˜ ์•ˆ์—๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ์œผ๋‹ˆ ์ด๊ฑธ ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์™€์•ผํ•œ๋‹ค.
1. docs์œผ๋กœ commenting ๋ฌถ์Œ์„ ๊ฐ€์ ธ์˜ค๊ณ , ์—ฌ๊ธฐ์„œ doc์œผ๋กœ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์ž
2. doc์˜ ๋ฐ์ดํ„ฐ๋ฅผ commentData๋ผ๊ณ  ์ •์˜
3. temp_html์— ๋‚ด๊ฐ€ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์€ ํ˜•์‹์„ ์‚ฌ์šฉํ•ด์„œ commentData์˜ text๋ฅผ ๊บผ๋‚ด์„œ ๋„ฃ์–ด์ค€๋‹ค.
4. ๊ทธ๋ฆฌ๊ณ  commentList div์— temp_html์„ ๋„ฃ์–ด์ฃผ๋ฉด ์™„์„ฑ!

๐Ÿ‘€ ๋Œ“๊ธ€์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜ํ•˜์ž

๋Œ“๊ธ€์ด firestore์— add์™€ get์€ ์ž˜๋˜์—ˆ์ง€๋งŒ, addํ•˜์ž๋งˆ์ž ํŽ˜์ด์ง€์— ๋ฐ˜์˜๋˜์ง€ ๋ชปํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ”๋กœ๋ฐ”๋กœ ์ถ”๊ฐ€๋˜๋ฉด ์ข‹๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
์ด๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ Firestore์˜ ์‹ค์‹œ๊ฐ„ ๋ฆฌ์Šค๋„ˆ onSnapshot() ์ด๋‹ค!
์•„๋ž˜ ๋งํฌ์—์„œ ๋”ฐ๋กœ ์„ค๋ช…ํ–ˆ์œผ๋‹ˆ ๋”ฐ๋ผ๊ฐ€๋ณด์ž
๐Ÿ”— https://velog.io/@minjee2758/Firestore-%EC%8B%A4%EC%8B%9C%EA%B0%84-%EB%B0%98%EC%98%81-onSnapshot-query-orderBy

0๊ฐœ์˜ ๋Œ“๊ธ€