I've been experimenting with Firebase in my web project and I'm curious about the best way to retrieve data from Firestore specific to the currently logged-in user.
This is what I attempted:
import firebase from 'firebase';
const uid = firebase.auth().currentUser.uid;
firebase.firestore().collection('posts').add({
title: this.title,
content: this.content,
user: uid
})
firebase.firestore().collection('posts').where("user", "==", uid).get()
.then(snapshots => {
this.posts = snapshots.docs.map(doc => {
const data = doc.data();
return {
id: doc.id,
title: data.title,
content: data.content
}
})
})
.catch(function(error) {
console.log("Error getting documents: ", error.message);
});
While this method works, it feels a bit amateurish. I'm wondering if there's a more professional approach for achieving this task.