【发布时间】:2018-08-12 07:13:26
【问题描述】:
在检查用户是否登录后,我正在尝试从我的 firebase 加载数据。我尝试通过承诺来执行此操作,但出现此错误
whenAuth.then 不是函数
我还尝试从componentDidMount() 和render() 方法加载数据,但查询没有等待用户初始化。
componentWillMount() {
let whenAuth =
firebase.auth().onAuthStateChanged((user) => {
const userProfile = firebase.auth().currentUser;
if(user){
this.setState({
authenticated: true,
name : userProfile.displayName,
email : userProfile.email,
uid : userProfile.uid,
})
} else {
this.setState({
authenticated: false,
})
return <Redirect to="/"/>
}
})
whenAuth.then(()=>{
const previousCards = this.state.cards;
firebase.database().ref().child('app').child('cards')
.orderByChild('uid').equalTo(this.state.uid)
.once('value', snap => {
snap.forEach(childSnapshot => {
previousCards.push ({
id: childSnapshot.key,
cardDesc: childSnapshot.val().cardDesc,
cardPreis: childSnapshot.val().cardPreis,
cardHeading: childSnapshot.val().cardHeading,
cardBewertung: childSnapshot.val().bewertung,
cardImage: childSnapshot.val().imageUrl,
standOrt: childSnapshot.val().ort,
imageArr: childSnapshot.val().imageArr,
})
this.setState ({
cards: previousCards,
})
})
})
})
}
提前感谢您的帮助
【问题讨论】:
-
你确定
firebase.auth().onAuthStateChanged()会返回一个承诺吗? -
firebase.auth().onAuthStateChanged()不返回 Promise:firebase.google.com/docs/reference/js/… -
不,它不会返回一个承诺,但我真的不知道如何在其他地方执行另一个函数
-
.onAuthStateChange 不返回承诺,但它是异步的 - 即,应用程序最初会将用户视为
null,一秒钟后它将有一个用户对象。因此,根据 .onAuthStateChanged填充您的 UI。
标签: javascript reactjs firebase ecmascript-6 promise