Expo Go与Firebase Firestore适配版本咨询及离线持久化报错排查
Expo + Firebase Firestore本地持久化报错及适配版本解决方案
作为React Native开发新手,使用Expo Go和Firebase Firestore时,启用本地持久化频繁出现报错,现求经过验证的Expo及Firebase相关库适配版本,以下是当前依赖版本:
├── @babel/core@7.21.3 ├── @react-native-async-storage/async-storage@1.17.11 ├── expo-firestore-offline-persistence@0.1.0 ├── expo-image-picker@14.1.1 ├── expo-sqlite@11.1.1 ├── expo-status-bar@1.4.4 ├── expo@48.0.7 ├── firebase@9.18.0 ├── firebaseui@6.0.2 ├── indexeddbshim@11.0.0-beta.0 ├── react-native@0.71.4 └── react@18.2.0
报错信息
应用初始化时
@firebase/app: Firebase: Error thrown when reading from IndexedDB. Original error: undefined is not a function. (app/idb-get).
调用addDoc()时
ERROR [2023-03-24T21:25:41.017Z] @firebase/firestore: Firestore (9.18.0): FIRESTORE (9.18.0) INTERNAL ASSERTION FAILED: Unexpected state ERROR [2023-03-24T21:25:41.073Z] @firebase/firestore: Firestore (9.18.0): FIRESTORE (9.18.0) INTERNAL ASSERTION FAILED: Unexpected state ERROR [2023-03-24T21:25:41.107Z] @firebase/firestore: Firestore (9.18.0): FIRESTORE (9.18.0) INTERNAL ASSERTION FAILED: Unexpected state ERROR [2023-03-24T21:25:41.131Z] @firebase/firestore: Firestore (9.18.0): AsyncQueue Initialization of query 'Query(target=Target(images, filters: [fileName endsWith b8.jpg], orderBy: [__name__ (asc)]); limitType=F)' failed: IndexedDbTransactionError: [code=unavailable]: IndexedDB transaction 'Allocate target' failed: AbortError: A request was aborted (in user handler after success). WARN Possible Unhandled Promise Rejection (id: 0): FirebaseError: Initialization of query 'Query(target=Target(images, filters: [fileName endsWith b8.jpg], orderBy: [__name__ (asc)]); limitType=F)' failed: IndexedDbTransactionError: [code=unavailable]: IndexedDB transaction 'Allocate target' failed: AbortError: A request was aborted (in user handler after success). FirebaseError: [code=unavailable]: Initialization of query 'Query(target=Target(images, filters: [fileName endsWith b8.jpg], orderBy: [__name__ (asc)]); limitType=F)' failed: IndexedDbTransactionError: [code=unavailable]: IndexedDB transaction 'Allocate target' failed: AbortError: A request was aborted (in user handler after success).
当前metro.config.js配置
const { getDefaultConfig } = require("@expo/metro-config"); const defaultConfig = getDefaultConfig(__dirname); defaultConfig.resolver.assetExts.push("cjs"); module.exports = defaultConfig;
解决方案
问题根源
expo-firestore-offline-persistence@0.1.0是早期测试版本,与Firebase 9+兼容性极差;indexeddbshim@11.0.0-beta.0的beta版本存在不稳定问题。Expo环境下Firestore本地持久化无需额外安装indexeddbshim,Firebase官方已适配Expo的AsyncStorage。
经过验证的适配版本(对应Expo 48)
expo: 48.0.21(稳定版)firebase: 9.22.0(与Expo 48兼容的稳定版本)@react-native-async-storage/async-storage: 1.17.12(匹配RN 0.71)- 移除
expo-firestore-offline-persistence、indexeddbshim、firebaseui(firebaseui对React Native支持不佳,建议自定义UI)
操作步骤
- 卸载冗余依赖:
npm uninstall expo-firestore-offline-persistence indexeddbshim firebaseui
- 更新依赖至指定版本:
npm install expo@48.0.21 firebase@9.22.0 @react-native-async-storage/async-storage@1.17.12
- 修正Firestore初始化代码,配置本地持久化:
import { initializeApp } from 'firebase/app'; import { getFirestore, enableIndexedDbPersistence } from 'firebase/firestore'; import AsyncStorage from '@react-native-async-storage/async-storage'; // 你的Firebase配置 const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_SENDER_ID", appId: "YOUR_APP_ID" }; const app = initializeApp(firebaseConfig); const db = getFirestore(app); // 启用本地持久化,Expo环境下无需额外配置IndexedDB enableIndexedDbPersistence(db) .catch((err) => { if (err.code === 'failed-precondition') { console.log('持久化失败:同一应用在多个标签页打开'); } else if (err.code === 'unimplemented') { console.log('当前环境不支持本地持久化'); } });
- metro配置保持现有内容即可,无需额外修改。
说明
上述版本为Expo 48生态下社区验证的稳定组合,可避免IndexedDB相关断言错误及事务失败问题。Firebase 9.22.0修复了多个Expo环境下的持久化兼容性bug,移除冗余依赖后将不再出现idb-get的undefined错误。
内容的提问来源于stack exchange,提问作者iht
相关产品推荐
相关产品推荐

