我在这里要做的是实际上只是从我的firestore读取数据。我按照RNF的文档进行了所有安装,但是当我尝试从我的文档中获取数据或仅引用firestore集合时,出现错误:
我尝试改变规则(因为我认为这是涉及的规则),但没有运气。 这是我的数据库结构:
以下是我的代码:
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>
);
}
}
如何解决此错误?
答案 0 :(得分:0)
firestore的默认设置是禁用访问。
因此,如果您未设置访问规则,则无法获取数据。
将它们更改为此 - 但仅用于测试:
service cloud.firestore {
match /databases/{database}/documents {
// Match all documents, recursively, with a wildcard and the "=**" recursive modifier
match /{document=**} {
allow read, write;
}
}
}
然后在它工作之后 - 阅读有关here的更多信息。