Reputation: 1
Im being able to get all the documents from my firebase firestore but I want to add a delete button and my function is not working. For what I understand I have to declare the ID inside .doc() but when I console log it (item.id) says undefined. My item.title and item.content are working perfectly. The only way I managed to get this working was hardcoding the string that appears in firestore instead of putting item.id, but of course that's not the idea..
export default function AdminPage() {
const [newsList, setNewsList] = React.useState('')
React.useEffect(() => {
firestore().collection('user').limit(8).get()
.then(data => {
setNewsList(data.docs.map(doc => doc.data()))
return newsList})
.catch(err => {
console.log('Error getting documents', err);
})
}, [newsList]);
return(
<View>
<View style={styles.eeuu}>
{newsList.map(i => {return (
<View>
<Text key={i.title}>{i.title}</Text>
<Text key={i.content}>{i.content}</Text>
<TouchableOpacity onPress={() => firestore().collection('users').doc('item.id').delete()}>
<Text>DELETE </Text>
</TouchableOpacity>
</View>
)})}
</View>
</View>
</View>
)
}
Upvotes: 0
Views: 2234
Reputation: 317382
doc.data()
contains only the document fields and not the ID. If you want your view to be able to use the document ID, you will need to pass it along to the view in the state.
setNewsList(data.docs.map(doc => {return {...doc.data(), id: doc.id} }))
Note here that doc.id
contains the ID and it's being added to the mapped object being made available to the view in the id
property.
Upvotes: 1