【问题标题】:'Uncaught TypeError: firebaseApp.firestore is not a function' when integrating Firebase to Vue.js 2将 Firebase 集成到 Vue.js 2 时出现“未捕获的 TypeError:firebaseApp.firestore 不是函数”
【发布时间】:2021-11-13 07:48:28
【问题描述】:

检查时出现此错误>> 未捕获的类型错误:firebaseApp.firestore 不是函数。 使用的依赖项:“vuefire”:“^3.0.0-alpha.2”,“firebase”:“^9.0.2”, 我的 db.js 文件:

import * as Firebase from "firebase/app";
import 'firebase/firestore';
import { initializeApp } from "firebase/app";

const firebaseApp = Firebase.initializeApp({
      apiKey: "x***************************",
      authDomain: "a**********.firebaseapp.com",
      projectId: "f****demo-****",
      storageBucket: "f**********.appspot.com",
      messagingSenderId: "2***********",
      appId: "1:**********:web:2**************"
});
export const db = initializeApp(firebaseApp.firestore());

【问题讨论】:

    标签: firebase vue.js google-cloud-firestore vuefire


    【解决方案1】:

    在 Firebase SDK v9 中,API 界面改为使用模块化、可摇树的代码。预计您看到的几乎所有文档或示例代码都是针对需要更新的 v8 或更早版本的 Firebase SDK 编写的。

    阅读更多about migrating here

    对于您的具体情况,您需要使用getFirestore() 方法,传入相关的FirebaseApp 实例:

    import { getFirestore } from "firebase/firestore";
    
    export const db = getFirestore(firebaseApp);
    

    虽然,因为这是默认的未命名实例,你也可以直接使用:

    import { getFirestore } from "firebase/firestore";
    
    export const db = getFirestore();
    

    【讨论】:

    • 这是一个非常好的答案。它有效。然后我得到 Uncaught TypeError: Cannot read properties of undefined (reading 'collection')。我应该在 db.js 中启动一个文档实例吗?
    • 要获得收藏,您需要使用import { collection } from "firebase/firestore";collection(db, 'collectionName')API referencedocumentation 更详细地介绍了这些步骤。
    猜你喜欢
    • 2021-11-22
    • 2018-07-13
    • 2020-11-03
    • 2018-04-15
    • 2017-01-20
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多