์์์ ํ๋ฆ๋๋ก ๋ฐฑ์ ์ฐ๊ฒฐํ๋ค๋ณด๋๊น ๋ด๊ฐ ์ด๋ป๊ฒ ํ๋์ง ๊ธฐ์ต์ด ์๋๋ค..
ํฐ์ผ๋ฌ๋ค ์ถ์ด์ ์ง๊ธ์ด๋ผ๋ ๊ธฐ๋ก ์์ํ๊ธฐ
https://codingapple.com/unit/firebase-installation-with-npm/
์ฐ์ ์ด๊ธฐ ์ค์ ์ ์ด๊ฑฐ ๋ณด๊ณ ํ๋ค.
blog.html (๊ธ ๋ชฉ๋ก์ด ๋ณด์ด๋ ํ์ด์ง)
์คํฌ๋ฆฝํธ ๋ถ๋ถ
const db = firebase.firestore(); //db์ฐ๊ฒฐํ๊ณ
console.log(db);
db.collection('posts') //posts collection๊ฐ์ ธ์์ ์ ์ฒด ๋ค ๋ฟ๋ ค์ฃผ๊ธฐ
.get().then((result) => {
result.forEach((postInfo) => {
console.log(postInfo);
const convertDate = (date) => { //firestore์ timestamp๋ก ์ ์ฅ๋ ๋ ์ง format
// whatever formatting you want to do can be done here
var d = date.toString()
return d.substr(4, 11);
}
var date = convertDate(postInfo.data().uploadDate.toDate())
const data = postInfo.data().id;
const postTemplate = `
<div class="col-xl-4 col-md-6">
<div class="post-item position-relative h-100">
<div class="post-content d-flex flex-column">
<h3 class="post-title"><a class="goToPost" style="color: #364D59;" href="post.html?${data}">${postInfo.data().title}</a></h3>
<div class="meta d-flex align-items-center">
<div class="d-flex align-items-center">
<span>${postInfo.data().sub}</span> //๋ฐ์ดํฐ ์ด๋ฐ์์ผ๋ก ๋ฟ๋ ค์ค
<!--์๋ต-->
`
document.querySelector('.posts-list').insertAdjacentHTML('afterbegin', postTemplate); //์์์ ๋ง๋ postTemplate์ posts-list class์ ์ฐ๊ฒฐํด์ ๋ฃ๊ธฐ!
const aTag = document.getElementsByClassName('goToPost')[0]; //className์ผ๋ก ๊ฐ์ ธ์ฌ๋ ๋ฐฐ์ดํ์์ด๊ธฐ๋๋ฌธ์ [0] ๋ถ์ฌ์ผ ์๋ฌ ์๋ธ!
aTag.addEventListener('click', () => { //์ ๋ชฉ ๋๋ฅด๋ฉด ์ด๋ํ ์ ์๋๋ก ์ด๋ฒคํธ ์ถ๊ฐ
location.href = `post.html?${data}`; //์์ด๋ ๋๊ธฐ๊ธฐ
},false);
});
});
</script>
html ๋ถ๋ถ
<div class="row gy-4 posts-list"></div>
ํฌ์คํธ์ ๊ฒ์๊ธ ํ์ดํ์ ํด๋ฆญํ๋ฉด ๋์ค๋ post.html
post detail ์ด ๋์ค๊ณ ๋งํฌ ๋์ด์จ ์์ด๋๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ด
const receivedData = location.href.split('?')[1]; //ํ์ฑ
console.log(receivedData);
const db = firebase.firestore();
console.log(db);
db.collection('posts').doc(receivedData).get().then((postInfo) => {
//์์ด๋๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
const convertDate = (date) => {
// whatever formatting you want to do can be done here
var d = date.toString()
return d.substr(4, 11);
}
var date = convertDate(postInfo.data().uploadDate.toDate())
const data = postInfo.data().id;
const postTemplate = `
<article class="blog-details">
<h2 class="title">${postInfo.data().title}</h2>
<div class="meta-top">
<ul>
<li class="d-flex align-items-center"><i class="bi bi-clock"></i> ${date}</li>
</ul>
`;
document.querySelector('.blogPost').insertAdjacentHTML('afterbegin', postTemplate);
});
๋ฐ์ํ
'๐ข PROJECTS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ธ์ฃผ์ผ์ง 4 AWS S3๋ก ๋ฐฐํฌํ๊ธฐ (0) | 2022.09.18 |
---|---|
์ธ์ฃผ์ผ๊ธฐ 3 ์น์ฌ์ดํธ ๋ค๊ตญ์ด ์ฒ๋ฆฌ (0) | 2022.09.18 |
vscode์์ ํ๋ก ํธ ์์ ํ ๋ .gitignore ํ์ผ ์ค์ (0) | 2022.08.12 |
์ธ์ฃผ ์ผ์ง 1 (0) | 2022.08.09 |
Spring web service camp week1: front-end(html, css, javascript.. etc) *์์ฑ์ค (0) | 2022.07.11 |
๋๊ธ