ํ ํ์ด์ง์ ๊ฒ์๊ธ์ ์ถ๊ฐํ๋ ๊ธฐ๋ฅ์ด ํ์ํ๊ธฐ ๋๋ฌธ์, firebase์์ ๊ฒ์๊ธ ๋ฐ์ดํฐ ๊ด๋ฆฌ์ ๋๊ธ ๊ด๋ฆฌ๋ฅผ ์ถ๊ฐํ๊ธฐ๋ก ํ๋ค!

firebase ์ฝ์์์ ํ๋ก์ ํธ ๋ง๋ค๊ธฐ๋ฅผ ํด๋ฆญํด TeamIntroduction์ด๋ผ๋ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ค.

์ค๋ฅธ์ชฝ ๋ฐ์ ์๋ firestore Database์ ๋ค์ด๊ฐ ๊ธฐ์ด ์ธํ
์ ํ์
๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง๋ค๊ธฐ ๋ฒํผ์ ํด๋ฆญํด seoul์ ์ ํํ๊ณ ์์ฑํ๋ค.

๊ทธ๋ฆฌ๊ณ ๊ท์น์์ false๋ฅผ true๋ก ๋ณ๊ฒฝํด์ค๋ค

ํ๋ก์ ํธ ์ค์ ์ ๋ค์ด๊ฐ๋ค

sdk ์ค์ ์ ๊ตฌ์ฑ ์ผ๋ก ๋ณ๊ฒฝํด์ค๋ค.
๋์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ๋, ์ด ์๋์ ์ฝ๋๋ฅผ ๋ณต์ฌํด์ ๋ฃ์ผ๋ฉด firestore Database๋ฅผ ์ฌ์ฉํ ์ ์๋ค!
html ์ฝ๋์ <script> ๋ฅผ <script type="module"> ๋ก ๋ณ๊ฒฝํด์ฃผ์
๊ทธ๋ฆฌ๊ณ ์ ์ด์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผํ๊ธฐ ๋๋ฌธ์ ์๋์ ์ฝ๋๋ฅผ ํํ ๋ฃ์!(์๋ฃ์๋ค๊ฐ ์ ์๋๋์ง ํ์ฐธ ๊ณ ๋ฏผํ๋ค..)
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
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 ์ธ์คํด์ค ์ด๊ธฐํ
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
"Firebase ๊ตฌ์ฑ ์ ๋ณด ์ค์ " ์ฌ๊ธฐ์๋ค๊ฐ ์๊น ์ฐ๋ฆฌ์ Firestore์ ์๋ SDK ๊ตฌ์ฑ ์ฝ๋(api key ๋ค์ด์๋)๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค!
์ด์ ์ค๋น๋ ๋๋ฌ๋ค! ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด๋ณด์
addDoc์ ์ฌ์ฉํ๋ฉด, firestore base์ ๋ฐ์ดํฐ๋ฅผ ์
๋ก๋ํ ์ ์๋ค.
์์ ์ฝ๋์ ํจ๊ป ์์๋ณด์
$("#postingbtn").click(async function () {
let isValid = true;
let title = $("#title").val();
let content = $("#content").val();
let link = $("#link").val();
let file = $("#file")[0].files.length;
//Firestore ์ ์ฅํ๊ธฐ
let doc = {
'title': title,
'content': content,
'link': link,
'file': file
};
await addDoc(collection(db, "postings"), doc);
alert('์ ์ฅ ์๋ฃ!');
window.location.reload();
});
์๋ body ์์ ์๋ div์ ์์์ id๊ฐ "#postingbtn"์ธ ๊ณณ์ ์ ์ฉ์ด ๋๋ค๋ ๋ป์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๊ฐ๊ฐ ๊ฑฐ๊ธฐ์์ id์ ๋ง๋ ๋ณ์๋ค์ let์ ํ์ฉํด ์ ์ฅํ๋ค
doc์ด๋ผ๋ Dictionary๋ฅผ ํ์ฉํด ์์๋ค์ ์ ์ฅํด์ฃผ๊ณ ,
์ด๋ฅผ await addDoc(collection(db, "postings"), doc); firestore์ ์ฝ๋์
์์ postings๋ผ๋ ์ด๋ฆ์ db์ doc์ ์ ์ฅํ๋ค. ๋ผ๋ ๋ป์ ๊ฐ์ง ์ฝ๋๋ก ์ ์ฅํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์์ผํ๋ฏ๋ก
// ํ์ผ URL ์์ฑ
let fileURL = "";
if (file) {
let reader = new FileReader();
reader.onload = function (e) {
fileURL = e.target.result; // data URL ํํ๋ก ํ์ผ์ URL์ ๊ฐ์ ธ์ด
console.log("ํ์ผ URL:", fileURL);
// Firestore์ ๋ฐ์ดํฐ ์ ์ฅ
let doc = {
'title': title,
'content': content,
'link': link,
'fileURL': fileURL // ํ์ผ URL์ Firestore์ ์ ์ฅ
};
addDoc(collection(db, "postings"), doc)
.then(() => {
alert('์ ์ฅ ์๋ฃ!');
window.location.reload();
})
.catch((error) => {
console.error("์๋ฌ ๋ฐ์: ", error);
alert("์ ์ฅ ์ค ์ค๋ฅ ๋ฐ์");
});
};
reader.readAsDataURL(file); // ํ์ผ์ ์ฝ์ด์ data URL๋ก ๋ณํ
}
fileURL = e.target.result;
์ ์ฝ๋๋ฅผ ํตํด ๋ณํํ์ฌ ์ ์ฅํ๋ค.
๊ทธ๋ฌ๋ฉด ์๋์ฒ๋ผ ์ ์ฅ๋ ๊ฑธ ๋ณผ ์ ์๋ค.

์ด์ ์ ์ฅํ์ผ๋ ๋ถ๋ฌ์ค๊ธฐ๋ง ํ๋ฉด ๋๋ค!
๋จผ์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์์ผํ๋ค. postings๋ก ๋ง๋ค์ด ๋ถ๋ ์ผ๋ postings์ ์๋ db๋ฅผ ๊บผ๋ด๋ณด์
let docs = await getDocs(collection(db, "postings"));
๊ฐ๊ฐ ๋ณ์์ ํ ๋นํด์ฃผ์
docs.forEach((doc) => {
let row = doc.data();
console.log(row);
let link = row['link'];
let title = row['title'];
let content = row['content'];
let fileURL = row['fileURL'];
๋ถ๋ฌ์จ ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก ํ์ด์ง์ ์ถ๊ฐํ๊ณ ์ถ์ผ๋๊น ์ด ๋ก์ง๋ ์ถ๊ฐํด๋ด ์๋ค
let temp_html =
`<div class="col">
<div class="card h-100">
<img src="${fileURL}">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${content}</p>
</div>
<div class="card-footer">
<small class="text-body-secondary">${link}</small>
</div>
</div>
</div>
`
$("#card").append(temp_html);
});
temp_html ๋ณ์์ ํ์์ ๋ง์ถ์ด ์๊น ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ๊ณ ,
body>์ ์๋ ๋ด๊ฐ ๋ฃ๊ณ ์ถ์ ๋ถ๋ถ์ div์ id์ด๋ฆ์ ์ด๋ ๊ฒ $("#card")๋ก ๋ฃ์ด์ฃผ๋ฉด ์์ฑ!
์ด๋ ๊ฒํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค๐