firebase 错误:FirebaseError:缺少权限或权限不足

Posted

技术标签:

【中文标题】firebase 错误:FirebaseError:缺少权限或权限不足【英文标题】:firebase Error : FirebaseError: Missing or insufficient permissions 【发布时间】:2021-10-31 21:09:17 【问题描述】:

所以我莫名其妙地得到了这些错误,我昨天使用了代码,一切都很好。

这就像一个社交媒体平台, 昨天我可以显示帖子,今天我不能。我仍然可以拍照并将其保存在运行良好的 Firebase 数据库中,但它不会在提要中自行发布。

这是代码:

function Profile(props) 
  const classes = useStyles();
  const [reason, setReason] = React.useState('');
  const [open, setOpen] = React.useState(false);

  const handleChange = (event) => 
    setReason(event.target.value);
  ;

  const handleClose = () => 
    setOpen(false);
  ;

  const handleOpen = () => 
    setOpen(true);
  ;


  const [userPosts, setUserPosts] = useState([]);
  const [user, setUser] = useState(null);
  const [following, setFollowing] = useState(false)
  useEffect(() => 
    const  currentUser, posts  = props;
    console.log( currentUser, posts );

    if (props.route.params.uid === firebase.auth().currentUser.uid) 
      setUser(firebase.auth().currentUser);
      setUserPosts(posts);
    else
            firebase.firestore()
            .collection("users")
            .doc(props.route.params.uid)
            .get()
            .then((snapshot) =>
                if(snapshot.exists)
                    setUser(snapshot.data())
                else
                    console.log('does not exist')
            
        )
        firebase.firestore()
        .collection("posts")
        .doc(props.route.params.uid)
        .collection("userPosts")
        .orderBy("creation", "asc")
        .get()
        .then((snapshot) =>
            let posts = snapshot.docs.map(doc => 
                const data = doc.data();
                const id = doc.id;
                returnid, ...data
      )
      setUserPosts(posts)
      )
    
    if(props.following.indexOf(props.route.params.uid) > -1)
        setFollowing(true);
    else
        setFollowing(false)
    
  ,[props.route.params.uid, props.following]);


  const onFollow = () =>
      firebase.firestore()
      .collection("following")
      .doc(firebase.auth().currentUser.uid)
      .set(
        following : [props.route.params.uid]
      )
  

const onLogout = () =>
    firebase.auth().signOut();




  if (user === null) 
    return <View />;
  
  return (
    <div className=classes.div>
      <div  >
            <Avatar  className=classes.avatar />
            <Typography className=classes.text > user.name  </Typography>
            <Typography className=classes.text > user.email </Typography>
            props.route.params.uid !== firebase.auth().currentUser.uid ? (
        
          <Container> 
              following ? (
                <Button
                  className=classes.btn
                  size="large"
                  variant="outlined"
                  onClick=() => onUnFollow()
                >Following</Button>
               ) : 
               (
                 <Button
                 className=classes.btn
                 size="large"
                 variant="outlined"
                 onClick=() => onFollow()
               >Follow</Button>
               )
           </Container>
            ) : <Button
              className=classes.btn
              size="large"
              variant="outlined"
              onClick=() => onLogout()
          >Logout</Button>
      
      <Card>
      /* //Verspätung */
        <CardContent>
              <Typography variant="h5" className=classes.cardTyp> Verspätung </Typography>
              <Container className=classes.cardContainer>
              <TextField
                id="time"
                label="Zeit"
                type="time"
                className=classes.cardTime
                defaultValue="07:30"
                InputLabelProps=
                   shrink: true,
              
                inputProps=
                  step: 300, // 5 min
              
      />
      <Button className=classes.cardBtn>Absenden</Button>
      </Container>
        </CardContent>

        /* //Krankenmledungen */
        <CardContent className=classes.cardKrankmeldung>
              <Typography variant="h5" className=classes.cardTyp> Krankenmledungen </Typography>
              <Container className=classes.cardContainer>
              <TextField
                id="date"
                label="Von"
                type="date"
                defaultValue="2017-05-24"
                className=classes.textField
                InputLabelProps=
                  shrink: true,
                
              />

              <TextField
                  id="date"
                  label="bis"
                  type="date"
                  defaultValue="2017-05-24"
                  className=classes.textField
                  InputLabelProps=
                    shrink: true,
                  
                />
      
        </Container>
        <Button className=classes.cardBtn>Absenden</Button>
        </CardContent>

        /* //Verspätung Abolung*/
        <CardContent>
              <Typography variant="h5" className=classes.cardTyp> Verspätung Abholung</Typography>
              <Container className=classes.cardContainer>
              <TextField
                id="time"
                label="Zeit"
                type="time"
                defaultValue="07:30"
                InputLabelProps=
                   shrink: true,
              
                inputProps=
                  step: 300, // 5 min
              
      />
      <Button className=classes.cardBtn>Absenden</Button>
      </Container>
        </CardContent>
      </Card>
      </div>
    </div>
)
    

const mapStateToProps = (store) => (
  currentUser: store.userState.currentUser,
  posts: store.userState.posts,
  following: store.userState.following
);

export default connect(mapStateToProps, null)(Profile);

【问题讨论】:

您可能需要检查您的 Firestore 安全规则。 【参考方案1】:

正如 Frank van Puffelen 指出的,您的问题在于 Firebase Firestore 安全规则。 如果您可以将帖子保存在数据库上,那么您似乎对该集合具有写入权限但没有读取权限。 您可以通过首先启用对该集合的所有读取和写入来测试更改的安全规则如何影响此问题

rules_version = '2';
service cloud.firestore 
  match /databases/database/documents 
    match /yourCollectionName/ID
        allow read, write: if true; 
    
  

,然后根据需要限制集合。

查看这些指南以对您的安全规则进行必要的修改和最佳实践。https://firebase.google.com/docs/firestore/security/get-startedhttps://firebase.google.com/docs/firestore/security/rules-structure

【讨论】:

@chin14 这个answer你的问题吗? 谢谢,成功了!

以上是关于firebase 错误:FirebaseError:缺少权限或权限不足的主要内容,如果未能解决你的问题,请参考以下文章

firebase firestore 离线持久化 FirebaseError

FirebaseError:权限缺失或不足。 -类星体和火力基地

[FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).]

React Native - FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created

FirebaseError:Firebase:未创建 Firebase 应用“[DEFAULT]” - 调用 Firebase App.initializeApp() (app/no-app)。 颤动

Flutter, FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - 调用 Firebase App.ini