
일단 내가 firebase database에 만든것
백엔드에서 데이터 담겨져서 날라오는거 생각하면 걍
note: {
name: "123"
뭐 다른 값들도 들어있겠지
}
이런느낌으로 받아들여져서 아아 ~~ 이런생각...
완전 잘 모르겠다 싶으면 그냥 대강 넣고 코드 따라넣고 몇번 데이터 이렇게 저렇게 넣어보면 감올듯싶다
내가 Import 한것들
import {app} from "@/firebase"
import {collection, doc, query, setDoc, getDocs, addDoc, updateDoc, deleteField, deleteDoc} from "firebase/firestore/lite";
import {getFirestore} from "firebase/firestore/lite";
const dbService = getFirestore();
async function getData(path) {
// 컬렉션의 모든 문서 가져오기
const getTestData = await getDocs(query(collection(dbService, path)))
return getTestData.docs.map(ele => ({
...ele.data()
}))
}
async function testGetData() {
// 데이터 아이디까지 알면 가져오는 방법
doc(dbService, 'test', 'case1')
}
// 데이터는 obj형태로 삽입해야함
async function addData(path, data) {
await addDoc(collection(dbService, path), data);
}
async function testAddData() {
// id가 랜덤값으로 들어감
await addDoc(collection(dbService, 'new'), {
// 들어갈 데이터는 객체형식
name: '안녕하세요',
bye: '반갑',
createdAt: Date.now(),
});
// set 으로 하면 Id를 지정할 수 있음
await setDoc(collection(dbService, 'new', 'createId'), {
// 들어갈 데이터는 객체형식
name: '안녕하세요',
bye: '반갑',
createdAt: Date.now(),
})
}
async function testDelData() {
//document 지우기
await deleteDoc(doc(dbService, "new", "rBBZTYyxe2rpjBj3LcxI"));
// 필드 지우기
const cityRef = doc(dbService, 'test', 'case1');
// Remove the 'capital' field from the document
await updateDoc(cityRef, {
id: deleteField()
});
}
const updateUser = async () => {
// 원하는 데이터 찾아서 수정함
const userDoc = doc(dbService, "new", 'rBBZTYyxe2rpjBj3LcxI');
try {
const res = await updateDoc(userDoc, {bye: 'zzz'});
console.log(res); // res는 undefined
} catch (e) {
console.log(e);
} finally {
console.log("end");
}
};
여기엔 주석을 다 달아놔서 딱히 설명할 필요가없을듯..
'firebase9' 카테고리의 다른 글
| firebase9 Authentication (0) | 2023.05.22 |
|---|---|
| firebase9 설치 vue3 (0) | 2023.05.22 |