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

Expo(React Native)集成Firebase 9.6.11遇权限不足问题求助

Expo + Firebase Firestore 权限错误解决方案

问题场景

Expo 46.0.9项目集成Firebase 9.6.11,Authentication功能正常,但调用Firestore时触发「Missing or insufficient permissions」错误。已配置完全开放的Firestore规则(如下),该规则在React.js项目中有效,但在Expo项目中无效:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Expo的Firebase配置代码:

import AsyncStorage from '@react-native-async-storage/async-storage';
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { API_KEY, AUTH_DOMAIN, PROJECT_ID, STORAGE_BUCKET, MESSAGING_SENDER_ID, APP_ID } from "@env"

import {
  initializeAuth,
  getReactNativePersistence
} from 'firebase/auth/react-native';

const firebaseConfig = {
  apiKey: API_KEY,
  authDomain: AUTH_DOMAIN,
  projectId: PROJECT_ID,
  storageBucket: STORAGE_BUCKET,
  messagingSenderId: MESSAGING_SENDER_ID,
  appId: APP_ID
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// initialize auth
const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage)
});

// initialize firestore
const db = getFirestore(app);

export { auth, db };

排查与解决步骤

  • 确认Firestore规则已发布
    修改规则后必须点击Firebase控制台中的「发布」按钮,规则才会生效。重新进入Firestore规则页面,确认当前生效的规则是你配置的开放规则。

  • 核对Firebase项目配置
    检查@env导入的PROJECT_ID是否与Firebase控制台的项目ID完全一致,项目ID错误会导致连接到错误的Firebase项目,进而触发权限问题。

  • 清除Expo缓存并重启
    Expo可能缓存了旧的配置或规则,执行命令清除缓存后重启项目:

    expo r -c
    
  • 验证Firestore调用逻辑
    确保调用Firestore时使用的是正确初始化的db实例,示例查询代码:

    import { db } from './path/to/your-firebase-config-file';
    import { collection, getDocs } from "firebase/firestore";
    
    const fetchData = async () => {
      try {
        const snapshot = await getDocs(collection(db, "your-collection"));
        snapshot.forEach(doc => console.log(doc.data()));
      } catch (err) {
        console.error(err);
      }
    };
    

    排查是否存在未正确传入db实例的情况。

  • 调整Firebase版本适配Expo
    Expo 46.0.9与Firebase 9.6.11可能存在兼容性问题,尝试升级Firebase到9.22.0(适配Expo 46的稳定版本),或降级到9.5.0,重新安装依赖后测试。

内容的提问来源于stack exchange,提问作者Andre Oliveira Cunha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 16:46:37