【问题标题】:Writing to a firebase table in react native以本机反应写入firebase表
【发布时间】:2018-09-23 01:15:53
【问题描述】:

我有一个连接到 firebase 的 react 原生 android 应用。其中一个表“内容”具有以下架构: firebase schema

我有一个“张贴!”按钮,按下时我希望将用户的帖子写入此 Firebase 表。

<Button
  style={{height: 50, width: 20, paddingBottom: 10, paddingRight: 10}}
  title="Post It!"
  color='blue'
  onPress={this.uploadTextPost}
/>

“uploadTextPost”函数如下所示:

  uploadTextPost() {
    this.rootRef('Content/qwa4').set({
      Author: this.state.author,
      Content_Text: this.state.textPostContent,
      Content_Title: this.state.title,
      Content_Type: 'TEXT',
      Downvotes: this.state.downvotes,
      Plot_Point: this.state.textPlotPoint,
      Timestamp: this.state.timestamp,
      Upvotes: this.state.upvotes
    });
  }

访问this.state:

this.rootRef = firebase.database().ref();

    this.state = {
      author: 'John JJ James',
      textBookSubject: 'na',
      textPlotPoint: 'na',
      textPostContent: 'na',
      upvotes: 0,
      downvotes: 0,
      title: 'Hopefully this works',
      timestamp: '08/18/2018 19:59:23',
      index: 0,
      routes: [
        { key: 'first', title: 'Text Post' },
        { key: 'second', title: 'Art Post' },
        { key: 'third', title: 'Request'},
      ],
    };
  }

但是,当我在我的 Android 模拟器中按“发布”按钮对其进行测试时,nothing 会填充数据库。知道为什么吗?

【问题讨论】:

  • 两件事:(1)您能发布更多代码,以便我们可以确保 this.rootRef 符合您的预期吗? (2) 确保在远程调试器中请求没有任何问题
  • @JasonKao 我在构造函数中添加了“rootRef”变量。控制台中没有错误消息。有什么想法吗?
  • 刚刚发布了一个修复建议。

标签: javascript android firebase react-native mobile


【解决方案1】:

firebase.database().ref(PATH) 返回某个任意路径的引用。通过将this.rootRef 设置为firebase.database.ref(),您将this.rootRef 设置为数据库的根引用(这不是可调用函数)。

如果您不想更改uploadTextPost(),可以将this.rootRef 更改为firebase.database.ref,在这种情况下调用this.rootRef('Content/qwa4') 将等效于调用firebase.database.ref('Content/qwa4')

但是,我建议您保留this.rootRef 作为数据库的实际根引用(所以this.rootRef = firebase.database.ref())。在这种情况下,uploadTextPost() 应该是:

uploadTextPost() = {
  this.rootRef.child('Content/qwa4').set(...);
}

查看firebase.database.Reference 的文档,了解为什么child 方法在这里有效。

【讨论】:

  • 这成功了!非常感谢杰森真的很感激:)
  • 没问题,很高兴我能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 2019-03-18
  • 2018-01-19
  • 1970-01-01
  • 2019-06-01
相关资源
最近更新 更多