问题
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