3주차

두주·2023년 11월 20일
0

TIL

목록 보기
3/58

구글 firebase 활용

아래 태그로 사용하였을 때 사진과 같이 내가 원하는 순서에 들어가지 않음

브라우저 > db > 브라우저 순서로 불러오며 순서 개념이 사라지나 ??

실제로 firebase에는 순서가 상관없이 데이터가 저장되고 있었음

firebase에서 읽어오는 순서대로 불러오는 듯 ,,

album
<style>
    @import url('https://fonts.googleapis.com/css2?family=Nanum+Pen+Script&display=swap');

    * {
        font-family: 'Nanum Pen Script', cursive;
    }

    .mytitle {
        height: 250px;
        color: white;

        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        background-image: url('https://images.unsplash.com/photo-1511992243105-2992b3fd0410?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80');
        background-position: center;
        background-size: cover;
    }

    .mytitle>button {
        width: 150px;
        height: 50px;
        background-color: transparent;
        color: white;
        border: 1px solid white;
        border-radius: 5px;

        margin-top: 20px;
    }

    .mycards {
        width: 1200px;
        margin: 30px auto 0px auto;

    }

    .mypostingbox {
        width: 500px;
        margin: 30px auto 0px auto;
        padding: 20px;
        box-shadow: 0px 0px 3px 0px purple;
        border-radius: 5px;
    }

    .mybtn {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
    }

    .mybtn>button {
        margin-right: 10px;
    }
</style>
<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: "AIzaSyDtx9S7xPfxy_0OPsW4Nq-As2eLxhlh0e4",
        authDomain: "sparta-7cffd.firebaseapp.com",
        projectId: "sparta-7cffd",
        storageBucket: "sparta-7cffd.appspot.com",
        messagingSenderId: "918706180048",
        appId: "1:918706180048:web:b967aab3afc4680226f3d5",
        measurementId: "G-W3XF2WDQKS"
    };


    // 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();
    })

    let url = "http://spartacodingclub.shop/sparta_api/seoulair";
    fetch(url).then(res => res.json()).then(data => {
        let mise = data['RealtimeCityAir']['row'][0]['IDEX_NM']
        $('#seoul_mise').text(mise)
        console.log(mise);
    })

    let docs = await getDocs(collection(db, "albums"));
    docs.forEach((doc) => {
        let row = doc.data();
        
        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 h-100">
                <img src="${image}"
                    class="card-img-top" alt="...">
                <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">${date}</small>
                </div>
            </div>
        </div>`;
        $('#card').append(temp_html);

    });


    $("#savebtn").click(async function () {
        $('#postingbox').toggle();
    })

</script>
profile
야옹.

0개의 댓글