【问题标题】:Uncaught Error in snapshot listener: FirebaseError: Missing or insufficient permissions快照侦听器中未捕获的错误:FirebaseError:缺少权限或权限不足
【发布时间】:2021-03-10 20:37:47
【问题描述】:

我正在尝试使用 onSnapshot 侦听器从 Firebase 获取显示的帖子数据,但它显示此 Firebase 错误。有人可以帮助解决这个错误

App.js:

import { useState, useEffect } from 'react';
import './App.css';
import loggo from './icons/loggo.svg';
import Post from './Components/Post.js';
import { db } from './firebase';

function App() {
  const [posts, setPosts] = useState([]);
  useEffect(() => {
    db.collection('posts').onSnapshot(snapshot => {
      setPosts(snapshot.docs.map(doc => doc.data()));
    })
  }, []);

  return (
    <div className="App">
      <div className="app_header">
        <img className="header_Image" src={loggo} alt="insta" />
      </div>
      {
        posts.map(post => (
          <Post username={post.username} caption={post.caption} imageUrl={post.imageUrl} />
        ))
      }
    </div >
  );
}
export default App;

【问题讨论】:

    标签: javascript reactjs firebase web-development-server


    【解决方案1】:

    如果您使用自己的 Firestore,请转到管理控制台并将数据库规则更改为:

    allow read, write: if true;

    【讨论】:

      【解决方案2】:

      快照监听器至少需要读取权限。 (包括get和list) 所以你的“发布”收集规则至少应该是:

      allow read;
      

      因为它是一个帖子集合,我假设您希望只允许经过身份验证的用户写入权限:

      allow write : if request.auth != null;
      

      所以所有在一起就是:

       rules_version = '2';
       service cloud.firestore {
          match /databases/{database}/documents {
      
            //A read  is : get and list, 
            //A write is : create, update, and delete. you can separate them if you want
      
            match /posts/{post} {  //this applies to post collection
               allow read;
               allow write : if request.auth != null;
            }
      
            // other collections ....
          }
       }
      

      【讨论】:

        猜你喜欢
        • 2022-08-06
        • 2021-06-20
        • 2022-06-10
        • 2023-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-31
        相关资源
        最近更新 更多