๋๊ธ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ณ ์ถ์๋ค
<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์ ์ฌ์ฉํด ๋๊ธ์ ์
๋ ฅํ ์ ์๊ฒ ํ๋ค.
ํ์ผ์ ๋๋ ์ ๊ด๋ฆฌํ๋ฉด ๋ณด๊ธฐ๋ ์ฝ๊ณ , ์ฝ๋ ๊ด๋ฆฌํ๊ธฐ ์ ์ฉํ๊ธฐ ๋๋ฌธ์
์ฐ์ 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);
์คํฌ๋ฆฝํธ๊ฐ ์คํ๋๋ฉด ์๋์ผ๋ก ๋จผ์ ์คํ๋๋๋กํ๋ ์ฝ๋๋ฅผ ์ฐ์
$(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์ ์ ์ ์ฅ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.

์ด์ ๋ ์ ์ฅํ๊ฑธ ๊ฐ์ ธ์์ผํ๋ค.
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