You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Firebase Firestore时调用app.firestore报错不是函数的原因是什么?

报错原因
  • 你当前使用的是Firebase v8版本的命名空间式写法,但本地安装的Firebase依赖为v9及以上版本。v9开始官方重构为模块化API,默认不再兼容v8的app.firestore()链式调用语法。
  • 少部分情况是依赖安装异常,firebase包的核心模块与firestore子模块版本不匹配,或者本地依赖缓存损坏导致扩展方法未被正确挂载。
对应解决方案

方案1:继续使用现有v8风格写法

  • 方式一:降级Firebase到v8最终稳定版
    执行命令:npm i firebase@8.10.0
  • 方式二:使用v9+的兼容模式导入
    代码调整为:
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';

const firebaseConfig = {
    apiKey: "api key",
    authDomain: "asdjfg.firebaseapp.com",
    databaseURL: "aksdjgh.firebaseio.com",
    projectId: "test-asdkjkgfsg",
    storageBucket: "tasdgaest-asdfg-asgafg.appspot.com",
    messagingSenderId: "asgas5554",
    appId: "1:asgasdgasgef76b"
};

const app = firebase.initializeApp(firebaseConfig);
const db = app.firestore();
export default db

方案2:改用v9+模块化标准写法(官方推荐)

直接调整为适配新版本的代码,不需要兼容层:

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    apiKey: "api key",
    authDomain: "asdjfg.firebaseapp.com",
    databaseURL: "aksdjgh.firebaseio.com",
    projectId: "test-asdkjkgfsg",
    storageBucket: "tasdgaest-asdfg-asgafg.appspot.com",
    messagingSenderId: "asgas5554",
    appId: "1:asgasdgasgef76b"
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
export default db

如果调整后仍报错,可以删除node_modules和package-lock.json/yarn.lock后重新执行npm install安装依赖即可。

内容的提问来源于stack exchange,提问作者Nimish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 21:18:02