i wanna use firebase but i constantly get the
firebase.firestore is not a function
this is my js file where i want to add things to my firebase
import 'firebase/firestore';
import 'firebase/auth';
export function seedDatabase(firebase) {
function getUUID() {
// eslint gets funny about bitwise
/* eslint-disable */
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const piece = (Math.random() * 16) | 0;
const elem = c === 'x' ? piece : (piece & 0x3) | 0x8;
return elem.toString(16);
});
/* eslint-enable */
}
/* Series
============================================ */
// Documentaries
firebase.firestore().collection('series').add({
id: getUUID(),
title: 'Tiger King',
description: 'An exploration of big cat breeding and its bizarre underworld, populated by eccentric characters.',
genre: 'documentaries',
maturity: '18',
slug: 'tiger-king',
}); }
this is my js file where i want to connect to my firebase cloud
import { seedDatabase } from "../seed";
/// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import 'firebase/firestore';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "x",
authDomain: "x",
projectId: "x",
storageBucket: "x",
messagingSenderId: "x",
appId: "x"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
seedDatabase(app)
i searched through many stackpages but i didn't find my answer
CodePudding user response:
You are trying to use the version 8 style of API with the version 9 SDK. That won't work. The APIs are completely different in v9.
When following the Firebase documentation, you should make sure to use the v9 code samples by selecting the correct tab. For example, to add a new document, the code will look like this:
import { getFirestore, collection, addDoc } from "firebase/firestore";
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
// Add a new document with a generated id.
const docRef = await addDoc(collection(db, "series"), {
id: getUUID(),
title: 'Tiger King',
description: 'An exploration of big cat breeding and its bizarre underworld, populated by eccentric characters.',
genre: 'documentaries',
maturity: '18',
slug: 'tiger-king',
});
CodePudding user response:
if you are going to use version 9 it is configured in this way, you can initialize the configuration You will have to import Cloud Firestore js/firebase.js
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseApp = initializeApp({
apiKey: "XXXXXXXXXXXX",
authDomain: "XXXXXXXXXXXXXXX",
projectId: "XXXXXXXXXXXX",
storageBucket: "XXXXXXXXXXXXX",
messagingSenderId: "XXXXXXXXXX",
appId: "XXXXXXXXXXXXXX",
});
Initialize Firebase
const app = initializeApp(firebaseApp);
initialize Cloud Firestore and get a reference to the service
const db = getFirestore(app);
export { db };
now in to call the add() method
import { doc, setDoc } from "firebase/firestore"
import { db } from "js/firebase.js"; <<< ref
export function seedDatabase() {
try {
function getUUID() {
// eslint gets funny about bitwise
/* eslint-disable */
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const piece = (Math.random() * 16) | 0;
const elem = c === "x" ? piece : (piece & 0x3) | 0x8;
return elem.toString(16);
});
/* eslint-enable */
}
/* Series
============================================ */
// Documentaries
const { id } = await addDoc(collection(db, "series"), {
id: getUUID(),
title: "Tiger King",
description:
"An exploration of big cat breeding and its bizarre underworld, populated by eccentric characters.",
genre: "documentaries",
maturity: "18",
slug: "tiger-king",
});
} catch (error) {
console.log(error);
}
}