<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";
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyC4qDBozLNhesNOdzR9CzXqmRHGoJOmYFI",
authDomain: "fir-test-d8241.firebaseapp.com",
projectId: "fir-test-d8241",
storageBucket: "fir-test-d8241.appspot.com",
messagingSenderId: "720568215633",
appId: "1:720568215633:web:e25948c891b9f83b893070",
measurementId: "G-RS6MT0BLR5"
};
// Firebase 인스턴스 초기화
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
아래 코드를 스크립트 코드 안에 작성하여 데이터베이스와 연결한다.
$("#postingbtn").click(async function () {
// 넣고 싶은 데이터 선언 방식
// 변수 선언 = 값을 넣는 태그의 아이디값
let img1 = $('#img').val();
let title1 = $('#title1').val();
let content1 = $('#star').val();
let date1 = $('#reason').val();
let doc = {
// '파이어베이스에 뜨는 제목' : 저장된 값 쌍으로 선언
'이미지': img1,
'타이틀': title1,
'별': content1,
'코멘트': date1
};
//movies 라는 데이터집합을 생성하여 그 안에 데이터를 넣겠다
await addDoc(collection(db, "movies"), doc);
alert('저장완료');
window.location.reload();
})
파이어 베이스에 아래 형태로 사용자가 입력한 데이터가 담긴다.
