[英]firebase.firestore is not a function version 9
i wanna use firebase but i constantly get the我想使用 firebase 但我不断得到
firebase.firestore is not a function
firebase.firestore 不是 function
this is my js file where i want to add things to my firebase这是我的 js 文件,我想在其中添加东西到我的 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这是我想连接到 firebase 云的 js 文件
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我搜索了许多堆栈页面,但没有找到答案
You are trying to use the version 8 style of API with the version 9 SDK.您正在尝试将版本 8 样式的 API 与版本 9 SDK 一起使用。 That won't work.
那是行不通的。 The APIs are completely different in v9.
这些 API 在 v9 中完全不同。
When following the Firebase documentation, you should make sure to use the v9 code samples by selecting the correct tab.遵循 Firebase 文档时,您应该确保通过选择正确的选项卡来使用 v9 代码示例。 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',
});
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如果您要使用版本 9 以这种方式配置,您可以初始化配置您将必须导入 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初始化 Firebase
const app = initializeApp(firebaseApp);
initialize Cloud Firestore and get a reference to the service初始化 Cloud Firestore 并获取对服务的引用
const db = getFirestore(app);
export { db };
now in to call the add() method现在调用 add() 方法
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.