首页 > 解决方案 > 如何从firestore获取用户数据反应

问题描述

在使用 firebase 登录后,我必须获取特定用户的用户个人资料详细信息。现在,它正在返回我的 Firestore 中所有用户的详细信息。

在个人资料页面中获取详细信息的代码:

import React from 'react';
import firebase from 'firebase';

const firestore = firebase.firestore();

class ProfilePage extends React.Component {
    state = {
        profiledata : null
    }

    componentDidMount(){
        firestore.collection('profiledata')
            // .doc("JgT2LyOB4jqhMv9YMgrh")
            .get()
            
             .then((snapshot) => {
                const profiledata  = []
                snapshot.forEach(function(doc){
                    const data = doc.data();
                    profiledata.push(data);
                }
                )
                this.setState({profiledata :  profiledata})
             })
             }
             
            render(){
        return(
            <div className='profile'>
                <h1>User</h1>
                {
                    this.state.profiledata && 
                        this.state.profiledata.map( profiledata => {
                            return(
                                <div>
                                    <p>
                                        First Name : {String(profiledata.firstname)}
                                        Last Name : {String(profiledata.lastname)}
                                        Company Name : {String(profiledata.companyname)}
                                                                        
                                        </p>
                                </div>
                                
                            )
                        })
                }
                </div>
        )
    }export default ProfilePage;

这是我的输出:

first name : john lsatname: doe company: johndoe co
first name : Lisa lsatname: doe company: lisadoe co
first name : Roger lsatname: doe company: rogerdoe co

我的firestore集合中有三个用户,它在profilepage中给了我,现在的问题是我如何根据谁登录/注册来获取特定的用户详细信息?

有什么建议么??我怎样才能做到这一点?

标签: reactjsfirebasegoogle-cloud-firestore

解决方案


首先,您可以获取当前用户使用

firebase.auth().currentUser; 然后根据用户的 uid,您可以调用您的 firestore 集合以获取其他详细信息。


推荐阅读