【问题标题】:Reactjs promise load user data from firebaseReactjs 承诺从 firebase 加载用户数据
【发布时间】: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


【解决方案1】:

你的方法没有返回一个承诺。所以解决方案是在回调中调用你的逻辑。

myMethod() {
  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,
        })
      })
    })
  }

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="/"/>
    }

    // --> your function ends here
    this.myMethod();
  })
}

【讨论】:

  • 我试过了,但我认为首先调用了错误的函数...必须调用componentWillMount中的函数,完成后,我应该调用myMethod。
  • 很高兴为您提供帮助:)
猜你喜欢
  • 2021-03-30
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
相关资源
最近更新 更多