reactjs - 如何从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中给了我,现在的问题是我如何根据谁登录/注册来获取特定的用户详细信息?
有什么建议么??我怎样才能做到这一点?
解决方案
首先,您可以获取当前用户使用
firebase.auth().currentUser;
然后根据用户的 uid,您可以调用您的 firestore 集合以获取其他详细信息。
推荐阅读
- prolog - 是否有正式的系统可以让你做出类似人类的高阶陈述?
- node.js - PostgreSQL 的带有条件过滤的 ILIKE 查询
- python-3.x - 在 json 文件中添加值
- javascript - 在范围上进行双重 for 循环以从列中获取电子邮件并为该电子邮件 (GAS) 撰写表格
- vue.js - 在创建的钩子(vue.js)中模拟 setInterval
- c# - sr.ReadLine() 没有接收到正在发送的数据
- python-3.x - 使用 pyinstaller 编译后 youtube_dl 的导入错误
- regex - 不连续 2 次重复相同字符的正则表达式
- php - 当用户在 laravel 中下载外部文件时,更改或强制在网络上使用外部文件的名称
- python - 如何使用pandas groupby计算每列总数的百分比