本文介绍了react-native-firebase Firestore权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里要做的实际上是从Firestore中读取数据.我遵循了RNF文档中的所有安装步骤,但是当我尝试从文档中获取数据或只是引用到Firestore集合中时,会出现错误:

What I'm trying to do here is to actually just read data from my firestore. I followed all the installation from RNF's docs, but when I try to get data from my documents or just reference into firestore collection, an error shows up:

我尝试更改规则(因为我认为这是其中的规则),但是没有运气.这是我的数据库结构:

I tried changing the rules (as I thought it was the rules involved) but no luck.Here is my database structure:

下面是我的代码:

import React, { Component } from 'react';
import {View, Text, FlatList, Image, StyleSheet, Button, TouchableHighlight} from 'react-native';
import firebase from 'react-native-firebase';
import MainApp from './src/screen/test';

export default class App extends Component<{}> {
  constructor(){
    super();
    this.itemsRef = this.getRef('questions');
    this.state = {
      items:[],
      loading:true,
    };

  }

  setModalVisible(visible){
    this.setState(modalVisible:visible);
  }
  getRef(location){
    return firebase.firestore().collection('chat');
  }
  componentWillMount(){
    this.getItems(this.itemsRef);
  }
  componentDidMount(){
    //this.getItems(this.itemsRef);
  }
  getItems(itemsRef){
    firebase.firestore().collection('Users').get().then((snap)=>{
      let items =[];
      alert(snap);
      snap.forEach((childSnap)=>{
        items.push({
        title:childSnap.val().question,
        _key:childSnap.key
        });
        alert(childSnap.key)
      })
      this.setState({
        items,
        loading:false
      })
    })
  }
  pressRow(item){
    alert(item);
  }
  renderRow(item){
    return(
      <TouchableHighLight onPress={()=>{
        this.pressRow(item)
      }}>
      <View>
      <Text>{item.title}</Text>
      </View>
      </TouchableHighLight>
    )
  }
  addItem(){

  }
  render() {
    if (this.state.loading) {
    return null; // or render a loading icon
    }
    return (
      <View style={{ flex: 1 }}>
      <FlatList
        data={this.state.items}
      />
      <Button
          title={'Add TODO'}
          onPress={() => console.log()}
      />
  </View>
    );
  }
}

如何解决此错误?

推荐答案

firestore的默认设置是禁用访问权限.

The default setting for firestore is to disable access.

因此,如果您没有设置访问规则,则无法获取数据.

So if you don't have your access rules set up, you can't fetch data.

将它们更改为此-但仅用于测试:

Change them to this - but ONLY for testing:

service cloud.firestore { match /databases/{database}/documents { // Match all documents, recursively, with a wildcard and the "=**" recursive modifier match /{document=**} { allow read, write; } }}

service cloud.firestore { match /databases/{database}/documents { // Match all documents, recursively, with a wildcard and the "=**" recursive modifier match /{document=**} { allow read, write; } }}

然后生效-在此处中了解更多信息.

Then after it works - read more about here.

这篇关于react-native-firebase Firestore权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 15:16
查看更多