【问题标题】:reading data in react native with firebase使用firebase读取数据以反应本机
【发布时间】:2018-01-19 21:20:39
【问题描述】:

我目前无法使用我的 react 本机应用从 firebase 读取数据。这是我的代码和我的数据库结构的屏幕截图。测试非常基础:只读取用户的地址并将其设置为单击按钮时的状态。

好吧,这根本行不通,因为返回值为 null...我做错了什么?

export default class Test extends React.Component {
state = {headerText: 'caca'}

onButtonPressed() {
this.database = firebase.database();
const userId = firebase.auth().currentUser.uid;          
firebase.database().ref(`/users/bucheurs/${userId}/infos/adresse`).on("value", snapshot => {
this.setState({headerText: snapshot.val()})
 })
}
render() {

return (
 <View style={{
   flex: 1
 }}>
   <View style={styles.headerView}>
     <Header
      centerComponent={{
      text: this.state.headerText,
      style: {
      color: 'white',
      fontSize: 20
      }
    }} 
    style={styles.header}/>
  </View>
  <Button 
    title='GO' backgroundColor={secondary} 
    onPress={this.onButtonPressed.bind(this)}/>
</View>

) }; }

我的数据库结构:

【问题讨论】:

  • 什么不起作用?有错误吗?组件渲染失败?
  • 嘿,丹,组件渲染得很好,一切都很好;唯一的问题是返回的值为空。
  • 你能用完整的组件代码更新你的帖子吗?
  • 完成! ps 我正在为标题和按钮使用反应原生元素。
  • 您是否控制台记录了 userId 以确保它已定义?如果您可以访问 infos 中的所有值,请尝试访问 /users/bucheurs/${userId}/infos/

标签: typescript firebase react-native firebase-realtime-database


【解决方案1】:

今天早上失眠了一天,我找到了答案! :D

那是错误的“userId”部分。

ref 必须这样写:

ref(`users/bucheurs/ + ${userId} + /infos/adresse`)

希望这可以帮助一些 react-native / firebase 开发人员。谢谢大家的cmets。

【讨论】:

  • 好吧,这很奇怪......这是我能捕捉到值的唯一方法。这种语法也有效吗?
猜你喜欢
  • 2022-11-12
  • 2017-08-05
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
相关资源
最近更新 更多