【问题标题】:How can i get specific user data from firestore in React我如何从 React 中的 firestore 获取特定的用户数据
【发布时间】:2021-06-26 04:49:16
【问题描述】:

所以问题是,当我尝试使用 forEach 函数获取详细信息时,它会为我提供所有用户的详细信息,这些详细信息可在 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;

这是我的输出:

我的 firestore 集合中有三个用户数据,它在 profilepage 中给了我,现在的问题是如何获取特定的用户详细信息??

有什么建议吗??我该怎么做?

【问题讨论】:

  • 能否分享一下你的数据库结构?

标签: reactjs firebase firebase-realtime-database google-cloud-firestore


【解决方案1】:

在 profiledata 集合内的数据库结构中,文档很少。因此,如果您想获取特定的配置文件数据,您必须获取文档 ID。要检索文档 ID,您可以使用 doc.id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 2022-11-24
    • 2020-03-29
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多