Firebase๋ฅผ React ํ๋ก์ ํธ์์ ์ฌ์ฉํ๋ ค๋๋ฐ ์๊ธฐ๋ ๋ฌด์๋ฌด์ํ ์ค๋ฅ๋ค...
" ๋ ๋ง๊ฒ ํ๋๋ฐ??? " -> ๋ฒ์ 9๋ถํฐ ์ฌ์ฉ๋ฒ์ด ์์ ๋ฐ๋์์ต๋๋ค ^^
์ค๋ฅ ํด๊ฒฐ์ ์ฐพ์ ๊ณต์๋ฌธ์๋ฅผ ํญํดํ๋ฉฐ ์๊ฒ๋ ์๋ก์ด ์ฌ์ฉ๋ฒ๋ค์ ์ ๋ฆฌํ์ต๋๋ค.
๋ค๋ฅธ๋ถ๋ค์ ๊ณ ์ํ์ง ๋ง์ธ์
๊ณต์๋ฌธ์ ์ ์์คํจ์ ๋๋ผ๊ฒ ๋ ์ด์ผ๊ธฐ.. START
import firebase from "firebase/app";
import "firebase/auth";
const firebaseConfig = {
...
};
firebase.initializeApp(firebaseConfig);
export const authService = fierbase.auth();
๋ถ๋ช ๋๊ฐ์ด ์ณค๋๋ฐ ๋ฐ์ํ๋ ์ค๋ฅ..
ํ์ด์ด๋ฒ ์ด์ค ์ธ์ฆ ๋ชจ๋์ ์ฌ์ฉ ํ๋ ๊ณผ์ ์์ ๋ฐ์ํ๋ค
Failed to compile.
./src/fbase.js
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
์๊ณ ๋ณด๋ Firebase V9 ๋ถํฐ ๋ฌ๋ผ์ง import ๋ฐฉ์๋๋ฌธ์ด์๋ค ใ
ใ
๋๋ฆ ์ต๊ทผ์ ๋ฐํํ ์ฑ
์ด์์์๋.. ์ด๋ฐ ํฐ ๋ณํ๋ผ๋..
๊ทธ๋ฆฌํ์ฌ ๋ฐ๋ ๋ฒ์ ์ ๋ง๊ฒ ์๋ก ์ฝ๋๋ฅผ ์งฐ๋ค
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
...
};
const app = initializeApp(firebaseConfig);
export const authService = getAuth();
firebase.auth
๋์ getAuth()
์ฌ์ฉ
firebase
๊ฐ ์๋ intiailizeApp
import
์๋
data = await authService.createUserWithEmailAndPassword(email, password);
์ด๋ ๊ฒ createUserWithEmailAndPassword
ํจ์์ signInWithEmailAndPassword
ํจ์๋ฅผ ์ฌ์ฉํ๋๋ฐ
TypeError: fbase
__WEBPACK_IMPORTED_MODULE_0__
.authService.createUserWithEmailAndPassword is not a function
์ด๋ฐ ์ค๋ฅ๊ฐ ๋์๋ค...
ํจ์๊ฐ ์๋๋ผ๊ณ ์ธ์์ ํ๋ค
์ด ์ญ์ ํ์ด์ด๋ฒ ์ด์ค v9๋ก ์ธํด ํจ์ ์ฌ์ฉ๋ฐฉ์์ด ๋ฌ๋ผ์ง๊ฑฐ์๋คใ ใ
ํด๊ฒฐ
import {
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
} from "firebase/auth";
๋ฅผ ์ถ๊ฐํ๊ณ
data = await createUserWithEmailAndPassword(
authService,
email,
password
);
์ ํํ๋ก createUserWithEmailAndPassword
ํจ์์ signInWithEmailAndPassword
ํจ์๋ฅผ ์ฌ์ฉํ๋ค
export const firebaseInstance = firebase;
Failed to compile.
src/fbase.js
Line 23:33: 'firebase' is not defined no-undef
Search for the keywords to learn more about each error.
์ต์ํ ์ค๋ฅ..
getAuth()
##1 ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก getAuth
๋ฅผ ์ด์ฉํ์ฌ ํด๊ฒฐ ๊ฐ๋ฅํ๋ค ใ
ใ
export const firebaseInstance = getAuth();
GoogleAuthProvider
๊ฐ ์๋๋ ์ค๋ฅprovider = new firebaseInstance.authGoogleAuthProvider();
Unhandled Rejection (TypeError): Cannot read property 'GoogleAuthProvider' of undefined
ํ์ด์ด๋ฒ ์ด์ค ์์
๋ก๊ทธ์ธ๋ง ํ๋ฉด ์ค๋ฅ๊ฐ ์๊ธด๋ค
๋ฌผ๋ก ์ด๊ฒ๋ ์ญ์! ๋ฒ์ ์ ์ฐจ์ด ^^!
ํด๋น ํจ์๋ฅผ ์ฌ์ฉํ ํ์ผ ์์
import {
signInWithPopup,
GoogleAuthProvider,
} from "firebase/auth";
๋ฅผ ์ถ๊ฐํด์ฃผ๊ณ
provider = new GoogleAuthProvider();
๋ก ํจ์ ์ฌ์ฉ ๋ฐฉ์์ ๋ฐ๊ฟ์ค๋ค
signInWithPopup
์ค๋ฅconst data = await authService.signInWithPopup(provider);
Unhandled Rejection (TypeError): fbaseWEBPACK_IMPORTED_MODULE_0.authService.signInWithPopup is not a function
ํจ์๊ฐ ์๋๋ผ๊ณ ๋์จ๋ค (์ด๋์ ๊ฐ ๋ง์ด ๋ดค๋ค)
์ ##4 ๊ทธ๋ฆฌ๊ณ ##2 ์ ๋น์ทํ๊ฒ ํด๊ฒฐํ๋ค
const data = await signInWithPopup(authService, provider);
Failed to compile.
src/fbase.js
Line 25:26: 'firebase' is not defined no-undef
Search for the keywords to learn more about each error.
ํ์ฅ์ ์ธ ์ค๋ฅ.. ๊ทผ๋ฐ ์ด๋์ ๋ง์ด ๋ณธ ์ค๋ฅ๋ค.
##1
์ ํด๊ฒฐ๋ฐฉ๋ฒ์ ์์ฉํด์ ํด๊ฒฐํ๋ค
//import "firebase/firestore" ๋์
import { getFirestore } from "firebase/firestore";
//export const dbService = firebase.firestore(); ๋์
export const dbService = getFirestore();
import {dbService} from "fbase";
...
collection("nweets").add({...});
Unhandled Rejection (TypeError): fbase
__WEBPACK_IMPORTED_MODULE_1__
.dbService.collection is not a function
์์ฃผ ์ต์ํ๊ฒ ์๊ธด ์ค๋ฅ๊ฐ ๋ ๋์๋ค.
์ด์ ๋ ๋ด๊ฐ ๋ด๋ธ๋ก๊ทธ๋ฅผ ๋ณด๊ณ ํด๊ฒฐ์ ์๋ํ๊ธฐ ์์ํ๋ค..
์ด๋ฒ์๋ ##4๋ฅผ ์ฐธ๊ณ ํจ
//import {dbService} from "fbase"; ๋์
import {collection} from "firebase/firestore"
๋ฅผ ์ํฌํธํด์ฃผ๊ณ
//dbService.collection("nweets").add({...}) ๋์
collection("nweets").add({...});
๋ก ํจ์ ์ฌ์ฉ๋ฐฉ์์ ๋ณ๊ฒฝ
Unhandled Rejection (FirebaseError): Function collection() cannot be called with an empty path.
์ฅ ์๋๋ค...?
๊ฒฐ๊ตญ ๊ณต์๋ฌธ์ ๋ณด๊ณ ์๋ค. ใ
FireStore ์์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๋ ๋ฌธ์ ๋ฅผ ๋ดค๋๋
addDoc
๋ผ๋ ์์ ์๊ฐ์ง๋ ๋ชปํ ํจ์.. ๊ฐ ํ์ด๋์ค๋ค....์....
๊ฐํ ๋ด๊ฐ ์ด๋ ๊ฒ ๋ง์ ๋ณํ์ ์๋ํด๋ ๋๋๊ฑด์ง ๊ธด๊ฐ๋ฏผ๊ฐํ์ง๋ง ์ผ๋จ ๋ค์ ํด๋ดค๋ค
// await collection("nweets").add({...}); ๋์
await addDoc(collection(dbService, "nweets"), {
text: nweet,
createdAt: Date.now(),
});
์์์์ ์ฑ๊ณต@@!@!
๋๋ ์ด์ ๊ณต์๋ฌธ์์๊ฒ ์ธ์คํ์์๋ง๋๋ํ๊ต๊ณผ๋๊ธฐ ์ ๋์ ์น๋ฐ๊ฐ์ ๊ฐ์ง๊ฒ ๋์๋ค
const dbNweets = await dbService.collection("nweets").get();
Unhandled Rejection (TypeError): fbaseWEBPACK_IMPORTED_MODULE_2.dbService.collection is not a function
๋ ์ด์ ์ค๋ฅ๋ฅผ ์์ธกํ ์ ์๋ ๊ฒฝ์ง์ ์ด๋ฅด๋ ๋ค
collection.add
๋ addDoc(collection(...),{...})
๋ก ๋ฐ๋์๊ณ (##7 ์ฐธ๊ณ )
colletion.get
์ ๋ญ๋ก ๋ฐ๊ผ๋์ง ๊ณต์๋ฌธ์๋ฅผ ๋ณด๋ฌ๊ฐ์
๋น์ทํ๊ฒ getDocs
๋ฅผ ์ฐ๋๊น ์ด์ ๋ฐ๋ผ ๋ฐ๊ฟ์ค๋ค
//const dbNweets = await dbService.collection("nweets").get(); ๋์
const dbSweets = await getDocs(collection(dbService, "sweets"));
dbService.collection("nweets").onSnapshot((snpashot) => {
Failed to compile.
src/routes/Home.js
Line 21:24: 'snapshot' is not defined no-undef
Search for the keywords to learn more about each error.
์ด์ ๊ณต์๋ฌธ์ ์์ด ๋ชป์ฌ๋ ์ฌ๋์ด ๋์๋ค
import {onSnapshot} from "firebase/firestore";
...
// dbService.collection("nweets").onSnapshot((snapshot) => ์ด์ฉ๊ตฌ ๋์
onSnapshot(collection(dbService, "sweets"), (snapshot) => { ์ด์ฉ๊ตฌ
์๋ ๊ฒ ๋ฐ๊ฟง๋ค
db.doc()
v9์์ ์ฌ์ฉํ๊ธฐconst data = await dbService.doc(`nweets/%{nweetOnj.id}`);
์ญ์ ์ค๋ฅ๊ฐ ๋ฌ๋ค
์ฅ ๊ทผ๋ฐ ์กฐ๊ธ ๋ฏ์ ๊ฒฝ๊ณ ๋ฌธ๊ตฌ...
Unexpected reserved word 'await'.
await ์ ๋นผ๋ผ๋ค..?
์ฐพ์๋ณด๋ onSnapshot ํจ์๋ async-await ์ ๋ชป์ด๋จ๋ค
v8์์๋ ํ์ฉํ๋๋ฐ v9์์๋ ์์ ํ์ฉ๋ ์ํ๋ฏ
//const data = await dbService.doc(`nweets/%{nweetOnj.id}`); ๋์
const data = dbService.doc(`nweets/%{nweetOnj.id}`);
๊ทธ๋์ ๋นผ์ฃผ๋ฉด
TypeError: fbaseWEBPACK_IMPORTED_MODULE_0.dbService.doc is not a function
์ด์ ๋ ์ ๋ค์ด๋ฒ๋ฆฐ ์ด ๊ฒฝ๊ณ ๋ฌธ๊ตฌ,,
import { doc } from "@firebase/firestore"; //์ถ๊ฐ
...
//const data = dbService.doc(`nweets/%{nweetOnj.id}`); ๋์
const data = doc(dbService, `sweets/%{sweetOnj.id}`);
firebase v9 ์ ๋ง๋ ์ฌ์ฉ๋ฒ์ผ๋ก ๋ฐ๊ฟ์ฃผ์
์ด์ ๋ ์ ๋นํ ์ด๋ป๊ฒ ๋ฐ๊พธ๋ฉด ๋ ์ง ์์ธกํ๋ ์ง๊ฒฝ์ ์ด๋ฅด๋ ๋ค
const data = await dbService.doc(`nweets/${nweetObj.id}`).delete();
์ด๊ฑฐ ์ค๋ฅ๋์
const data = doc(dbService, `nweets/${sweetObj.id}`).delete();
์ด๋ ๊ฒ ํด๋
TypeError: Object(...)(...).delete is not a function
์ด๋ฐ ์ค๋ฅ๊ฐ ๋์
const data = deleteDoc(doc(dbService, `sweets/%{sweetOnj.id}`));
์ด๋ ๊ฒ ํด๊ฒฐ
ํ์ค ์์๋๋ฐ ์ญ์ ๊ฐ ์๋๋ค..?
๊ธฐ๋ฅ์์ฒด๋ ๊ตฌํํด์ ์ญ์ ๋ ๋๋๋ฐ ๊ทธ ๋์์ด ์๋ชป์ง์ ๋๋ ๋ฏํ๋ค
v9์ ์ค๋ช ํ๋ ๋๋ก ํ์์ (๋ฐ๋ฒ , ์ปฌ๋ ์ , ๋ฌธ์) ๋ก ๋ฐ๊ฟ๋ณด์
deleteDoc(doc(dbService, "sweets", sweetObj.id));
๊ตฌํ์ฑ๊ณต.. (์ฝ์ง ์๊ตฐ)
dbService.doc(`nweets/${nweetObj.id}`).update({ text: newSweet });
๋๊ปด์ง๋ค ์๋ฌ์ ๊ธฐ์ด
TypeError: fbaseWEBPACK_IMPORTED_MODULE_0.dbService.doc is not a function
์ค๋๋ง์ ๋ณธ ๋ด ์๋ฌ์น๊ตฌ
์์ฝ๊ฒ ์ฒ๋ฆฌํด์ฃผ์ (feat . ๊ณต์๋ฌธ์)
import { doc, updateDoc } from "@firebase/firestore";
...
updateDoc(doc(dbService, "sweets", sweetObj.id), { text: newSweet });
๊ฐ์๊ธฐ 7์ฅ์ผ๋ก ๊ฑด๋๋๋๋ค ํธํธ
await userObj.updateProfile({displayName: newDisplayName});
TypeError: fbaseWEBPACK_IMPORTED_MODULE_0.dbService.doc is not a function
ํด๊ฒฐ
import { updateProfile } from "@firebase/auth";
...
updateProfile(userObj, { displayName: newDisplayName });
๋๋ฌด TMI๋ผ์ ๊ธ ๋งจ ์๋๋ก ๋ด๋ ค๋ฒ๋ฆผ
์ผ์ฌ์ฐจ๊ฒ ์์ํ ๋
ธ๋ง๋ ์ฝ๋์ ํธ์ํฐ ํด๋ก ์ฝ๋ฉ
์ต๊ทผ ๋ฐํํ ์ฑ
์ธ๋ฐ ๊ทธ ์ฌ์ด์ ์
๋๋ ํ์ด์ด๋ฒ ์ด์ค์ v9๋ ์ฑ
๊ณผ ๋๋ฌด๋๋ ๋ฌ๋๋ค...
๋๊ฐ์ด ๋ฐ๋ผ์ณ๋ ๋๋ง ์ค๋ฅ๊ฐ ์๊ฒผ๋ค ใ
ใ
์๋๋ ํธ์ํฐ ํด๋ก ์ฝ๋ฉ ํ ๋ง TIL ์ ์์ฑํ๋ ค๊ณ ์๊ธ์ฐ๊ธฐ
๋ฅผ ๋๋ ๋๋ฐ..
๋๋ฅผ ๋ฐ๊ธฐ๋ ์๊ธฐ์น ๋ชปํ ์ค๋ฅ๋ค.....
๊ทธ ์ค๋ฅ๋ค์ ํด๊ฒฐํด๋๊ฐ๋ ์ด์ผ๊ธฐ..๊ฐ ๋์ด๋ฒ๋ฆฐ.. ํด๋ก ์ฝ๋ฉ TIL์๋๊ฒ
์ ๋ง ์ต๊ทผ์ ์
๋๋๊ฑฐ๋ผ ๊ทธ๋ฐ์ง ์ธํฐ๋ท์ ์ ๋ณด๋ ๋ณ๋ก ์์๋ค
๊ทธ๋ ๊ฒ.. ๋๋ ๊ณต์๋ฌธ์์ ์นํด์ก๋ค.
์ฌ๋ํฉ๋๋ค ์ ์๋