首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >firebase.firestore不是函数版本9

firebase.firestore不是函数版本9
EN

Stack Overflow用户
提问于 2022-08-10 15:57:50
回答 2查看 42关注 0票数 0

我想使用火力基地,但我经常得到

firebase.firestore不是一个函数

这是我的js文件,我想在这里把东西添加到我的防火墙中。

代码语言:javascript
复制
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文件,我想在这里连接到我的防火墙云。

代码语言:javascript
复制
    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)

我翻阅了许多书页,但没有找到答案。

EN

回答 2

Stack Overflow用户

发布于 2022-08-10 16:07:28

您正在尝试在版本9 SDK中使用版本8的API样式。那不管用。v9中的API完全不同。

在遵循Firebase文档时,应该通过选择正确的选项卡来确保使用v9代码示例。例如,对于添加一个新文档,代码如下所示:

代码语言:javascript
复制
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',
});
票数 1
EN

Stack Overflow用户

发布于 2022-08-10 16:20:58

如果要使用版本9,则可以以这种方式配置它,可以初始化必须导入Cloud /Firebase.js的配置

代码语言:javascript
复制
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

代码语言:javascript
复制
const app = initializeApp(firebaseApp);

初始化Cloud并获取对服务的引用

代码语言:javascript
复制
const db = getFirestore(app);
export { db };

现在调用add()方法

代码语言:javascript
复制
  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);
      }
     
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73309345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档