我如何从React中的Firestore获取特定的用户数据 [英] How can i get specific user data from firestore in React
问题描述
所以问题是,当我尝试通过使用 forEach
函数获取详细信息时,它为我提供了在Firestore上可用的所有用户详细信息./p>
这是我的代码,用于在个人资料页面"中获取详细信息:
import从'react'导入React;从"firebase"导入firebase;const firestore = firebase.firestore();ProfilePage类扩展了React.Component {状态= {profiledata:null}componentDidMount(){firestore.collection('profiledata')//.doc("JgT2LyOB4jqhMv9YMgrh").得到().then((快照)=> {const profiledata = []snapshot.forEach(function(doc){const data = doc.data();profiledata.push(data);})this.setState({profiledata:profiledata})})}使成为(){返回(< div className ='profile'>< h1>用户</h1>{this.state.profiledata&&this.state.profiledata.map(profiledata => {返回(< div>< p>名称:{String(profiledata.firstname)}姓氏:{String(profiledata.lastname)}公司名称:{String(profiledata.companyname)}</p></div>)})}</div>)}导出默认的ProfilePage;
这是我的输出:
我的Firestore集合中有三个用户数据,它在配置文件页面中提供了我的信息,现在的问题是如何获取特定的用户详细信息?
有什么建议吗??我该怎么办?
在profiledata集合内的数据库结构中,几乎没有文档.因此,如果要获取特定的配置文件数据,则必须获取文档ID.要检索文档ID,可以使用doc.id.
So the problem is, when i'm trying to get the details by using forEach
function it gives me all the user's details which is available on the firestore..let me explain with code:
Here is my code for getting details in Profile Page :
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;
Here is my Output :
There are three user data in my firestore collection and it gives me in profilepage, now the problem is how can i get the specific user details??
Any suggestions?? How can i do that?
In your database structure inside the profiledata collections there are few docs. Therefore if you want to get a specific profile data you have to get the document id. To retrieve the document id you can use doc.id.
这篇关于我如何从React中的Firestore获取特定的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!