






추후 프로젝트 진행시, 이 코드 그냥 복사하면 됨!


// 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);
<script type="module">
⚠️ 브라우저 자체의 보안적인 설정으로 인해 외부파일 js가 index.html에 연결되지 않아 html코드 내에서 해당 js코드를 사용해야하는 것이 강제된다고 한다!


<button id="postingBtn" type="button" class="btn btn-primary">
기록하기
</button>
$("#postingBtn").click(async function () {
let doc = {};
await addDoc(collection(db, "콜렉션이름"), doc);
})
$("#postingBtn").click(async function () {
let doc = { name: "sieun", age: "22" };
await addDoc(collection(db, "albums"), doc);
});


$("#postingBtn").click(async function () {
let image = $("#image").val();
let title = $("#title").val();
let content = $("#content").val();
let date = $("#date").val();
let doc = {
image: image,
title: title,
content: content,
date: date,
};
await addDoc(collection(db, "albums"), doc);
});

alert("저장 완료!"); // 기록하기 버튼을 눌렀을 때 알림띄우기
window.location.reload(); // 화면 새로고침 하기
$("#postingBtn").click(async function () {
let image = $("#image").val();
let title = $("#title").val();
let content = $("#content").val();
let date = $("#date").val();
let doc = {
image: image,
title: title,
content: content,
date: date,
};
await addDoc(collection(db, "albums"), doc);
alert("저장 완료!");
window.location.reload();
});

우린 파이어스토어를 사용하기 위해
<script type="module">를 해 주었다!
🥹 하지만 type에 module이 붙는 순간,<button>을 수행할 수 없다!!
💡 따라서 직접 button에다가 click을$("#postingBtn").click이런식으로 해주면 된다!
<button onclick="openClose()">추억 저장하기</button>
<button id="saveBtn">추억 저장하기</button>
$("#saveBtn").click(async function () {
$("#postingBox").toggle();
});
function openClose() {
$("#postingBox").toggle();
}
➡️ 이렇게 해주면 토글 기능이 정상적으로 작동되는 것을 확인할 수 있다!
Firestore Database에서 데이터 가져와서 카드를 생성해보자
let docs = await getDocs(collection(db, "콜렉션이름"));
docs.forEach((doc) => {
let row = doc.data();
console.log(row);
});

function makeCard() {
let image = $("#image").val();
let title = $("#title").val();
let content = $("#content").val();
let date = $("#date").val();
let temp_html = `
<div class="col">
<div class="card">
<img
src="${image}"
class="card-img-top"
alt="..."
/>
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${date}</p>
<p class="card-text">${content}</p>
</div>
</div>
</div>`;
$("#card").append(temp_html);
}
여기에 붙여주자!
let docs = await getDocs(collection(db, "albums"));
docs.forEach((doc) => {
let row = doc.data();
console.log(row);
let image = $("#image").val();
let title = $("#title").val();
let content = $("#content").val();
let date = $("#date").val();
let temp_html = `
<div class="col">
<div class="card">
<img
src="${image}"
class="card-img-top"
alt="..."
/>
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${date}</p>
<p class="card-text">${content}</p>
</div>
</div>
</div>`;
$("#card").append(temp_html);
});
그 후, row의 값이 들어가게 코드를 수정해주자
let image = row["image"];
let title = row["title"];
let content = row["content"];
let date = row["date"];

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>나만의 추억앨범</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="./style.css" />
<script type="module">
// 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 구성 정보 설정
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyDtbTUSAXiXsJv1bvxx4y909zisqSo5uuk",
authDomain: "happy-882bd.firebaseapp.com",
projectId: "happy-882bd",
storageBucket: "happy-882bd.appspot.com",
messagingSenderId: "915470516981",
appId: "1:915470516981:web:6249afe8f44cfe8213fbbc",
measurementId: "G-4C8TY07XXM",
};
// Firebase 인스턴스 초기화
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
$("#postingBtn").click(async function () {
let image = $("#image").val();
let title = $("#title").val();
let content = $("#content").val();
let date = $("#date").val();
let doc = {
image: image,
title: title,
content: content,
date: date,
};
await addDoc(collection(db, "albums"), doc);
alert("저장 완료!");
window.location.reload();
});
$("#saveBtn").click(async function () {
$("#postingBox").toggle();
});
$(document).ready(function () {
let url = "http://spartacodingclub.shop/sparta_api/weather/seoul";
fetch(url)
.then((res) => res.json())
.then((data) => {
let temp = data["temp"];
if (temp > 20) {
$("#seoulWeather").text("더워요");
} else {
$("#seoulWeather").text("추워요");
}
});
});
let docs = await getDocs(collection(db, "albums"));
docs.forEach((doc) => {
let row = doc.data();
console.log(row);
let image = row["image"];
let title = row["title"];
let content = row["content"];
let date = row["date"];
let temp_html = `
<div class="col">
<div class="card">
<img
src="${image}"
class="card-img-top"
alt="..."
/>
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${date}</p>
<p class="card-text">${content}</p>
</div>
</div>
</div>`;
$("#card").append(temp_html);
});
</script>
</head>
<body>
<div class="mytitle">
<h1>나만의 추억 앨범</h1>
<p>현재 서울의 미세먼지 : <span id="msg"></span></p>
<button id="saveBtn">추억 저장하기</button>
</div>
<div class="mypostingBox" id="postingBox">
<div class="form-floating mb-3">
<input
type="email"
class="form-control"
id="image"
placeholder="앨범 이미지"
/>
<label for="floatingInput">앨범 이미지</label>
</div>
<div class="form-floating mb-3">
<input
type="email"
class="form-control"
id="title"
placeholder="앨범 제목"
/>
<label for="floatingInput">앨범 제목</label>
</div>
<div class="form-floating mb-3">
<input
type="email"
class="form-control"
id="content"
placeholder="앨범 내용"
/>
<label for="floatingInput">앨범 내용</label>
</div>
<div class="form-floating mb-3">
<input
type="email"
class="form-control"
id="date"
placeholder="앨범 날짜"
/>
<label for="floatingInput">앨범 날짜</label>
</div>
<div class="myBtn">
<button id="postingBtn" type="button" class="btn btn-primary">
기록하기
</button>
<button type="button" class="btn btn-outline-primary">닫기</button>
</div>
</div>
<div class="mycards">
<div id="card" class="row row-cols-1 row-cols-md-4 g-4">
</div>
</div>
</div>
<script src="./index.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"
></script>
</body>
</html>