我想使用火力基地,但我经常得到
firebase.firestore不是一个函数
这是我的js文件,我想在这里把东西添加到我的防火墙中。
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',
}); }这是我的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)我翻阅了许多书页,但没有找到答案。
发布于 2022-08-10 16:07:28
您正在尝试在版本9 SDK中使用版本8的API样式。那不管用。v9中的API完全不同。
在遵循Firebase文档时,应该通过选择正确的选项卡来确保使用v9代码示例。例如,对于添加一个新文档,代码如下所示:
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',
});发布于 2022-08-10 16:20:58
如果要使用版本9,则可以以这种方式配置它,可以初始化必须导入Cloud /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",
});初始化Firebase
const app = initializeApp(firebaseApp);初始化Cloud并获取对服务的引用
const db = getFirestore(app);
export { db };现在调用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);
}
}https://stackoverflow.com/questions/73309345
复制相似问题