Firestore连接报错:Invalid database id [object Object]问题排查求助
近期克隆了自己开发的网站,并为其创建了新的Firebase项目。流程为执行create-react-app、firebase init后复制所有组件。网站运行正常,Firebase Auth也可正常实现用户登录、退出,但当尝试读写Firestore数据库时,出现如下错误:
@firebase/firestore: Firestore (9.19.1): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=invalid-argument]: Invalid database id [object Object]
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
已检查数据库的所有安全规则,除apiKey、authDomain等项目特定配置外,其余均与原项目一致。请问该错误的成因是什么?
这个错误的核心是Firestore初始化时传入了错误的数据库ID参数,错误提示里的“网络连接问题”是误导性的次要提示,真正问题出在代码配置上。
常见触发情况:
- 初始化Firestore时错误地将整个Firebase配置对象传给了
getFirestore/initializeFirestore方法,而非先初始化App再传入App实例。比如:// 错误写法 const db = getFirestore(firebaseConfig); // 正确写法 const app = initializeApp(firebaseConfig); const db = getFirestore(app); - 若使用多数据库实例,传入的数据库ID不是字符串类型而是对象,导致Firebase解析失败。
- 复制原项目代码时,原项目有自定义数据库ID配置,但新项目未同步调整,比如原项目指定了非默认数据库的ID字符串,新项目代码中误传了对象类型的值。
额外需要确认:
- 新Firebase项目是否已正确创建Firestore实例(包括选择对应地域)
- 代码中Firestore初始化逻辑是否和原项目完全匹配,有无参数传递错误
内容的提问来源于stack exchange,提问作者Spencer Brereton

