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

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

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的权限被拒绝。
这通常表明您的设备当前没有良好的互联网连接。客户端将在成功连接到后端之前以离线模式运行。

配置:

  1. import { initializeApp } from "firebase/app";
  2. import { getAuth } from "firebase/auth";
  3. import { getFirestore } from "firebase/firestore";
  4. import { getDatabase } from "firebase/database";
  5. import { initializeFirestore, CACHE_SIZE_UNLIMITED } from "firebase/firestore";
  6. const firebaseConfig = {
  7. apiKey: process.env.REACT_APP_FIREBASE_KEY,
  8. authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  9. projectId: process.env.REACT_APP_PROJECT_ID,
  10. storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  11. messagingSenderId: process.env.REACT_APP_SENDER_ID,
  12. appId: process.env.REACT_APP_APP_ID,
  13. measurementId: process.env.REACT_APP_MEASUREMENT_ID,
  14. databaseURL: process.env.REACT_APP_DATABASE_LINK
  15. };
  16. // Initialize Firebase
  17. const app = initializeApp(firebaseConfig);
  18. export const auth = getAuth(app);
  19. export const db = getFirestore(app, {
  20. experimentalForceLongPolling: true,
  21. });
  22. export default app;

数据获取代码:

  1. const [list, setList] = useState([]);
  2. const CollectionRef = collection(db, "users");
  3. const getList = async () => {
  4. try {
  5. const data = await getDocs(CollectionRef);
  6. const filteredData = data.docs.map((doc) => ({
  7. ...doc.data(),
  8. id: doc.id,
  9. }));
  10. setList(filteredData);
  11. } catch (err) {
  12. console.error(err);
  13. }
  14. };
  15. useEffect(() => {
  16. getList();
  17. }, []);
英文:

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:

  1. import { initializeApp } from "firebase/app";
  2. import { getAuth } from "firebase/auth";
  3. import { getFirestore } from "firebase/firestore";
  4. import { getDatabase } from "firebase/database";
  5. import { initializeFirestore, CACHE_SIZE_UNLIMITED } from "firebase/firestore";
  6. const firebaseConfig = {
  7. apiKey: process.env.REACT_APP_FIREBASE_KEY,
  8. authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  9. projectId: process.env.REACT_APP_PROJECT_ID,
  10. storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  11. messagingSenderId: process.env.REACT_APP_SENDER_ID,
  12. appId: process.env.REACT_APP_APP_ID,
  13. measurementId: process.env.REACT_APP_MEASUREMENT_ID,
  14. databaseURL: process.env.REACT_APP_DATABASE_LINK
  15. };
  16. // Initialize Firebase
  17. const app = initializeApp(firebaseConfig);
  18. export const auth = getAuth(app);
  19. export const db = getFirestore(app, {
  20. experimentalForceLongPolling: true,
  21. });
  22. export default app;

data fetching code:

  1. const [list, setList] = useState([]);
  2. const CollectionRef = collection(db, "users");
  3. const getList = async () => {
  4. try {
  5. const data = await getDocs(CollectionRef);
  6. const filteredData = data.docs.map((doc) => ({
  7. ...doc.data(),
  8. id: doc.id,
  9. }));
  10. setList(filteredData);
  11. } catch (err) {
  12. console.error(err);
  13. }
  14. };
  15. useEffect(() => {
  16. getList();
  17. }, []);
  18. </details>
  19. # 答案1
  20. **得分**: 0
  21. 我通过直接在代码中添加密钥值而不是使用环境文件来解决了这个问题。此外,我将“getFirestore”函数替换为“initializeFirestore”。这些更改成功解决了我遇到的问题。
  22. <details>
  23. <summary>英文:</summary>
  24. 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.
  25. </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:

确定