【发布时间】:2020-06-13 00:10:11
【问题描述】:
注意:我使用的是 react js,我所有的代码都在一个 react 组件类中。
我想检查某人是否是管理员,所以我做了这个功能:
makeDatabaseCall = () => {
let userDB = this.props.firestore.collection('user');
let queryUserDB = userDB.where('userIdentifier', '==', this.props.auth.uid).limit(1).get()
.then(snapshot => {
console.log(snapshot.docs[0].data().clearance)
return (
this.setState({
clearance: snapshot.docs[0].data().clearance
})
)
})
.catch(err => {
console.log('Error getting documents', err);
});
}
但是我需要使用this.props.auth.uid,所以需要等到auth加载完毕。
我不想把它放在我的 render() 中的 if 语句中,因为这会使我的代码效率低下(它会一直检查)。
如何使用promise的概念对firebase进行异步调用,查看是否加载了auth,然后然后运行上面的函数?
这也是我的验证码:
const mapStateToProps = (state) => {
return {
auth: state.firebase.auth
}
}
export default compose(
connect(mapStateToProps),
firestoreConnect([
{ collection: 'events',
collection: "user"
}
])
)(Dashboard)```
【问题讨论】:
-
您确实需要提供更多信息,例如,您在哪里加载
auth?显示上下文中使用的代码。另外,FWIW,我不完全理解为什么你认为如果你把代码放在if后面,它会一直检查,根据if语句的定义,它只会检查 if条件为真。 -
好吧,如果它在 render() 中,则需要继续检查。我将添加其他代码。谢谢!
标签: reactjs firebase promise firebase-authentication