FirebaseError: 在从Firestore获取数据时发生 [code=permission-denied] 错误。

huangapple go评论59阅读模式
英文:

FirebaseError: [code=permission-denied] when fetching data from Firestore

问题

I'm currently working on creating a serverless database using Firebase. However, I'm encountering an error whenever I attempt to fetch the data, even though my internet connection is stable and working properly.

Instead of receiving the expected data from Firestore, I got this error:

错误:

@firebase/firestore:Firestore(10.0.0):无法访问Cloud Firestore后端。连接失败1次。最近的错误:FirebaseError:[code=permission-denied]:资源项目find-jobs-5c4e7.firebaseapp.com的权限被拒绝。
这通常表明您的设备当前没有良好的互联网连接。客户端将在成功连接到后端之前以离线模式运行。

配置:

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getDatabase } from "firebase/database";
import { initializeFirestore, CACHE_SIZE_UNLIMITED } from "firebase/firestore";

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_SENDER_ID,
  appId: process.env.REACT_APP_APP_ID,
  measurementId: process.env.REACT_APP_MEASUREMENT_ID,
  databaseURL: process.env.REACT_APP_DATABASE_LINK
};

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

export const auth = getAuth(app);
export const db = getFirestore(app, {
  experimentalForceLongPolling: true,
});

export default app;

数据获取代码:

const [list, setList] = useState([]);
const CollectionRef = collection(db, "users");

const getList = async () => {
  try {
    const data = await getDocs(CollectionRef);
    const filteredData = data.docs.map((doc) => ({
      ...doc.data(),
      id: doc.id,
    }));
    setList(filteredData);
  } catch (err) {
    console.error(err);
  }
};

useEffect(() => {
  getList();
}, []);
英文:

I'm currently working on creating a serverless database using Firebase. However, I'm encountering an error whenever I attempt to fetch the data, even though my internet connection is stable and working properly.

Instead of receiving the expected data from Firestore, I got this error

The error:

> @firebase/firestore: Firestore (10.0.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=permission-denied]: Permission denied on resource project find-jobs-5c4e7.firebaseapp.com.
> 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.
>

the configuration:

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getDatabase } from "firebase/database";
import { initializeFirestore, CACHE_SIZE_UNLIMITED } from "firebase/firestore";

const firebaseConfig = {
  apiKey: process.env.REACT_APP_FIREBASE_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_SENDER_ID,
  appId: process.env.REACT_APP_APP_ID,
  measurementId: process.env.REACT_APP_MEASUREMENT_ID,
  databaseURL: process.env.REACT_APP_DATABASE_LINK
};

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

export const auth = getAuth(app);
export const db = getFirestore(app, {
  experimentalForceLongPolling: true,
});

export default app;

data fetching code:

const [list, setList] = useState([]);
  const CollectionRef = collection(db, "users");

  const getList = async () => {
    try {
      const data = await getDocs(CollectionRef);
      const filteredData = data.docs.map((doc) => ({
        ...doc.data(),
        id: doc.id,
      }));
      setList(filteredData);
    } catch (err) {
      console.error(err);
    }
  };

  useEffect(() => {
    getList();
  }, []);
      

</details>


# 答案1
**得分**: 0

我通过直接在代码中添加密钥值而不是使用环境文件来解决了这个问题。此外,我将“getFirestore”函数替换为“initializeFirestore”。这些更改成功解决了我遇到的问题。

<details>
<summary>英文:</summary>

I was able to resolve the issue by adding the key values in the code directly instead of using the env file. Additionally, I replaced the &quot;getFirestore&quot; function with &quot;initializeFirestore&quot;. These changes successfully addressed the problem I was experiencing.

</details>



huangapple
  • 本文由 发表于 2023年7月14日 05:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定