If I print current logged in user
console.log(firebase.auth().currentUser.uid)
Then it prints the user UID.
Then I tried to fetch current user information from firestore doing
import firebase from 'firebase/app'
import "firebase/auth"
import db from '../firebase/firebaseInit'
async getCurrentUser(){
const database =
db.collection('users').doc(firebase.auth().currentUser.uid)
const dbResults = await database.get()
console.log(dbResults.data())
}
Then it prints undefined
db.collection("users").where("userId", "==","pass the id you have").get().then((querySnapshot) => { querySnapshot.forEach((doc) => { console.log(doc.id, " => ", doc.data()); }); }).catch((error) => { console.log("Error getting documents: ", error); });
Did you set up a user collection in your firestore to store the users? Was the user uid stored in it? Firestore does not do this automatically so if you have not set one up then of course you get no results.
Was your db set up properly? Were you able to connect to your db with other methods using current db function?
Also for best practice, its a good idea to rename your database to docRef and dbResult to docSnap instead since it is a reference to a document!
If you have the user id stored in the collection as userId
, you can try:
db.collection('users').where('userId', '==', firebase.auth().currentUser.uid)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.