如何通过带有文档数据的ID在Firestore中更新和删除
到目前为止,它可以打印并且看起来像这样
EHhutrXlEH8x0yjsD1hi Object {
"AUTHOR": "VINCENT DIZON",
"CONTENT": "NIXXX",
"TITLE": "ADMIN",
}
jN7P58zK5JgJGOoDD2aV Object {
"AUTHOR": "Vincent Dizon",
"CONTENT": "EXAMPLE ANNOUNCEMENT",
"TITLE": "Sample Title",
}
我需要将文档ID传递给编辑功能,以便可以在Firestore中对其进行更新 这是我的代码如何通过ID来更新对象,使其像之前的代码一样
retrieveAnnouncement = () => {
const announcements = [];
const id = []
/* retrieve announcements */
firebase.firestore().collection('announcement').get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
announcements.push(doc.data(), doc.id);
});
// this will set announcements and trigger render.
this.setState({ content: announcements, docID: id });
/* console.log(announcements) */
})
.catch(err => console.log(err));
};
_renderAnnouncement = ({ item }) =>
//display the content of announcement
<Card>
<Text h3>{item.TITLE}</Text>
<Text h5>{item.CONTENT}</Text>
<View style={styles.container}>
<TouchableHighlight>
<View>
<Button title="Edit" onPress={() => console.log(item)} containerStyle={{marginLeft: 10, width: 80}} />
</View>
</TouchableHighlight>
{/* <Button title="Edit" onPress={this.editAnnouncement(item)} containerStyle={{marginLeft: 10, width: 80}} /> */}
<Button title="Delete" buttonStyle={{backgroundColor: 'red'}} containerStyle={{marginLeft: 10, width: 80}} />
</View>
</Card>
render () {
return (
<View>
<ScrollView>
<FlatList data={this.state.content} renderItem={this._renderAnnouncement} keyExtractor={item => item.id} />
</ScrollView>
</View>
)}