react-native-firebase firestore permission denied

一曲冷凌霜 提交于 2019-12-11 10:07:43

问题


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:

And below are my codes :

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>
    );
  }
}

How can I fix this error?


回答1:


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; } } }

Then after it works - read more about here.



来源:https://stackoverflow.com/questions/46947671/react-native-firebase-firestore-permission-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!