简体   繁体   English

firebase.firestore 不是 function 版本 9

[英]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.

相关问题 Firestore:Firebase.firestore 和 FirebaseFirestore.getInstance() 之间有什么区别 - Firestore: what's difference between Firebase.firestore and FirebaseFirestore.getInstance() Firebase / Firestore 将文档添加到子集合版本 9 - Firebase / Firestore Add Document to Sub collection Version 9 Firebase Function 查询和更新单个 Firestore 文档 - Firebase Function query and update a single Firestore document Firebase 云功能不更新 Firestore 数据库中的数据 - Firebase Cloud function not updating data in Firestore database 从 firebase 版本 8 升级到版本 9 compat - Service firestore 不可用 - Upgrading from firebase version 8 to version 9 compat - Service firestore is not available firebase.firestore.FieldValue.arrayUnion 不是 function - firebase.firestore.FieldValue.arrayUnion is not a function (Firebase Firestore)TypeError: n.indexOf 不是 function - (Firebase Firestore)TypeError: n.indexOf is not a function 无法连接本地 Firebase 模拟器套件托管与 Firestore(Web 版本 9) - Unable to connect local Firebase emulator suite Hosting with Firestore (Web version 9) Firebase Cloud Function CreateUser 上 Firestore 中的增量计数器 - Firebase Cloud Function increment counter in Firestore on CreateUser 为什么我收到“类型错误:文档不是函数”(Firebase 9、Firestore) - Why I receive "TypeError: doc is not a function" (Firebase 9, Firestore)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM