简体   繁体   English

如何检索当前从 Firestore/Firebase 登录的用户信息?

[英]How to retrieve the user's info that is currently logged in from Firestore/Firebase?

Hey guys I have a collection on Firestore users and each collection has displayName , email , and uid and trying to get the displayName of the user that is currently logged in.大家好,我有一个关于 Firestore users的集合,每个集合都有displayNameemailuid ,并试图获取当前登录用户的 displayName 。

At the moment the code below is fetching all the users correctly but not the specific user that logged in. The commented out code is kinda a solution that show around but I guess I am not doing it correctly.目前下面的代码正在正确获取所有用户,但不是登录的特定用户。注释掉的代码有点像一个解决方案,但我想我没有正确地做。 I have been trying for a while now and cant really find a way, it should be something simple but don't know what I am missing.我已经尝试了一段时间,但找不到真正的方法,它应该很简单,但不知道我错过了什么。

 const [users, setUser] = useState([])

  useEffect(() => {
    ;(async () => {
      const users: any = []
      const db = getFirestore()
      const userRef = collection(db, 'users')
      // const userName = getAuth().currentUser?.displayName
      // const userDocument = query(userRef, where('displayName', '==', userName))
      // console.log('name', userName)
      try {
        const snapshot = await getDocs(userRef)
        snapshot.docs.forEach((doc) => {
          // const { displayName } = doc.data()
          users.push({
            ...doc.data(),
            id: doc.id,
            // displayName,
          })
        })
        console.log(users)
        setUser(users)
      } catch (err: any) {
        console.log(err.message)
      }
    })()
  }, [])
  1. You need the logged in user's data / displayName from 'users' collection您需要“用户”集合中的登录用户数据/显示名称
  2. You have already connected with firebase/firestore that means you are maintaining a valid firebase config file / credentials您已经连接到 firebase/firestore,这意味着您正在维护一个有效的 firebase 配置文件/凭据
  3. If the above information is correct then this can be a solution for you如果以上信息正确,那么这可能是适合您的解决方案
  • Import firebase from the firebase config file从 firebase 配置文件导入firebase
  • Then get the current user's uid and userProfile然后获取当前用户的uid和userProfile
const uid = firebase.auth().currentUser.uid;
const currentUserProfile = firebase.firestore().collection("users").doc(uid);
  • Then you can get the required data in useEffect as required然后就可以根据需要在useEffect中获取需要的数据
useEffect(() => {
    currentUserProfile.get()
        .then((doc) => {
            // const currentUserInfo = doc.data();
            const currentUserDisplayName = doc.data().displayName;
            // console.log(currentUserDisplayName)
            // now you can update your state with this value from here if required 
        })
        .catch((err) => {alert("Error in displaying Name!")})

}, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 firestore 不显示登录用户的信息? - Why isn't firestore showing the logged in user's info? 如何从 firebase firestore 检索图像 - How to retrieve images from firebase firestore 如何在 Firestore 中有条件地检索当前用户的文档字段 - How to conditionally retrieve current user's doc fields in Firestore 如何使用 Firebase 从云 function 端查找用户是否已登录? - How to find user is logged in or not from cloud function side using Firebase? 如何检索 firebase firestore Android 上的数据? - How to retrieve data on firebase firestore Android? 如何将 integer 从 firebase firestore 显示到 Android Studio 的 TextView? - How to display integer from firebase firestore to Android Studio's TextView? Flutter:显示flutterstore中除当前登录用户外的所有用户 - Flutter: Display all users from flutterstore excluding the currently logged in user Flutter:获取当前登录用户以外的人的显示名称Firebase - Flutter: Get the display name of a person other than the user currently logged into Firebase Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents Firebase:如何让 Android 用户保持登录状态? - Firebase: How to keep an Android user logged in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM