'How to get document by current signed in user in Google Firestore
I am trying to get a specific document that is created when a user signs up. I made it so that when the use signs up, their firebase user ID is the name of the document.
I can't seem to figure out how to call that document.
I have tried: firebase.auth().currentUser.uid; uid userId (from computed) and several other related things to the uid.
When I look for a solution online I see the exact examples I have tried and I am wondering if there has been a change to how uid works maybe?
<script>
// eslint-disable-next-line
import firebase from "firebase";
// eslint-disable-next-line
import firestore from "firestore";
import db from "@/components/fbInit";
export default {
components: {},
data() {
return {
users: []
};
},
computed: {
userId() {
return firebase.auth().currentUser.uid;
}
},
created() {
db.collection("userProfiles")
.document(userId)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
const data = {
id: doc.id,
name: doc.data().name,
company: doc.data().company,
state: doc.data().state
};
this.users.push(data);
});
});
}
};
</script>
Usually I get no error but it doesn't load any document. Sometimes it will say "uid" or whatever I am trying to use is undefined.
Solution 1:[1]
This is the solution for anyone else wondering. unique is a field I set to match the user ID when a user is created. Alternatively I believe you can use author to get the author of the document.
var user = firebase.auth().currentUser
db.collection("userProfiles").where('unique', '==', user.uid).get()
.then(querySnapshot => {
querySnapshot.forEach(doc => { }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yewla |