【问题标题】:[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'currentUser.uid')][Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'currentUser.uid')]
【发布时间】:2020-07-30 09:18:35
【问题描述】:

当我尝试使用当前登录的用户将详细信息存储到 Firebase 时收到此警告。

我遵循了一个在线教程,但我遇到了这个问题,是否有任何替代方法可以用来实现这一点? 我读到了这个问题,但找不到解决方法,我看到这是由于用户没有登录但我确实登录了

var currentUser
class DecodeScreen extends Component {

  addToBorrow = async (booktitle, bookauthor, bookpublisher, bookisbn) => {

    currentUser = await firebase.auth().currentUser
    var databaseRef = await firebase.database().ref(currentUser.uid).child('BorrowedBooks').push()
    databaseRef.set({
      'title': booktitle,
      'author': bookauthor,
      'publisher': bookpublisher,
      'isbn': bookisbn
  })
  }

  state = {
    data: this.props.navigation.getParam("data", "NO-QR"),
    bookData: '',
    bookFound: false
 }
  _isMounted = false
 bookSearch = () => {
      query = `https://librarydb-19b20.firebaseio.com/books/${9781899606047}.json`,
      axios.get(query)
           .then((response) => {
               const data = response.data ? response.data : false
                   console.log(data)
                   if (this._isMounted){
                    this.setState({ bookData: data, bookFound: true })
                   }   
            })
  }
  renderContent = () => {
    if (this.state.bookFound) {
        return( 
          <View style={styles.container2}>

          <View style={styles.htext}>
          <TextH3>Title :</TextH3>
          <TextH4>{this.state.bookData.title}</TextH4>
          </View>

          <View style={styles.htext}>
          <TextH3>Author :</TextH3>
          <TextH4>{this.state.bookData.author}</TextH4>
          </View>

          <View style={styles.htext}>
          <TextH3>Publisher :</TextH3>
          <TextH4>{this.state.bookData.publisher}</TextH4>
          </View>

          <View style={styles.htext}>
          <TextH3>Isbn :</TextH3>
          <TextH4>{this.state.bookData.isbn}</TextH4>
          </View>

        </View>
        )
      }
      else {
        return(
          <View style={styles.loading}>
          <ActivityIndicator color="blue" size="large" />
        </View>
        )
      } 
 }


  componentDidMount(){
    this._isMounted = true
  }
  componentWillUnmount(){
    this._isMounted = false
  }
  render() {
    {this.bookSearch()}

  return (
    <View style={styles.container}>
        {this.renderContent()}
        <Button title='Borrow' onPress={() => this.addToBorrow(this.state.bookData.title, this.state.bookData.author, this.state.bookData.publisher, this.state.bookData.isbn)} />
    </View>
  );
  }
}

【问题讨论】:

    标签: reactjs firebase react-native firebase-realtime-database firebase-authentication


    【解决方案1】:

    首先,firebase.auth().currentUser 不是一个承诺,你不能await 它。我是either null or a user object

    其次,不能保证在立即加载页面时填充用户对象。您需要使用auth state observer 来了解用户对象在页面加载后何时首次可用。

    【讨论】:

    • 哦,好的,如何在我的代码中进行更改?对不起,我是新手,我完全不明白如何做出改变。
    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2019-02-24
    • 2022-12-21
    • 2023-02-18
    • 2022-12-03
    相关资源
    最近更新 更多