【问题标题】:Firebase User Specific Data with ReactFire使用 ReactFire 的 Firebase 用户特定数据
【发布时间】:2016-04-16 08:53:53
【问题描述】:

我正在尝试使用 ReactFire 来获取用户特定的数据并显示它。我已经按照Firebase's denormalized suggestions 组织了我的数据。下面看一下数据的结构。

"notes" : {
    "n1" : {
        "note" : "[noteData]",
        "created_at" : "[date]",
        "updated_at" : "[date]",
    }
},
"users" : {
    "userOne" : {
    "name" : "[userName]",
        "notes" : {
            "n1" : true
        }
    }
}

我可以在没有 ReactFire 的情况下使用以下代码从我的应用程序中读取和写入数据,只需使用 FB API(每次在记事本中键入击键时都会运行updateCode):

componentWillMount: function() {
  firebaseRef.child('users/' + authData.uid + '/notes').orderByChild('date_updated').on("child_added", function(noteKeySnapshot) {
  // Take each key and add it to an array
  usersNotesKeys.push(noteKeySnapshot.key());
  // For each note key, go and fetch the Note record with the same key
  firebaseRef.child('notes/' + noteKeySnapshot.key()).once("value", function(noteSnapshot) {
    // Add that full note object to an array + the parent key
    var data = noteSnapshot.val();
    usersNotesList.push({
        'created_at': data.created_at, 
        'updated_at': data.updated_at,
        'note':       data.note,
        'key':        noteKeySnapshot.key()
    });
  });
  this.setState({
    usersNotesList: usersNotesList
  });
}.bind(this)); 
},

updateCode: function(newCode) {
  firebaseRef.child('notes/' + this.state.item['key']).on('value', function(noteSnapshot, prevChildKey) {
  // Look through the current note list and find the matching key and update that key with the new content.

    var data = noteSnapshot.val();
    updatedItem = {
        'created_at': data.created_at, 
        'updated_at': data.updated_at,
        'note':       data.note,
        'key':        noteSnapshot.key()
        };
        // console.log(updatedItem);
    this.setState({
      item: updatedItem
    });
  }.bind(this));
}

从技术上讲,该代码有效。但是速度很慢。

当我使用 ReactFire 直接写入便笺时,无需通过用户,它工作得很好,而且更简单。但我需要说明是用户特定的。所以我想找到一种方法来使用这种数据结构的 ReactFire。这可能吗?

【问题讨论】:

  • 您为什么要在componentWillMount 上向usersNoteKeysusersNotesList 推送?
  • @arve0 - 好问题。 usersNotesList 是用户笔记的数组,一旦我去 FB 抓取它们。因此,该函数获取每个音符并将音符数据推送到 usersNotesList 数组中,然后可以在状态中访问。
  • componentWillMount 中写入数据有腥味。另外:我在updateCode() 中没有看到任何数据库更新。但是,我确实看到updateCode() 执行的每次,您都在附加一个新的侦听器。如果updateCode() 被反复调用,你最终会得到很多听众。
  • 谢谢,弗兰克!澄清一下,componentWillMount 中没有写入 FB 数据库,只是写入本地数组。 updateCode() 中的数据库更新代码是:noteRef.update({ "note": this.state.code, "updated_at": Firebase.ServerValue.TIMESTAMP }); 如果不附加新的侦听 updateCode(),您将如何更新代码?
  • @dave-dawson 啊哈,没有看到它在任何地方定义。删除updateCode 中的事件监听器并将componentWillMount 中的once 事件更改为value 事件怎么样?那不会做同样的事情吗?

标签: reactjs firebase


【解决方案1】:

重要的部分是你不想想听完整的笔记树。这可以通过断言注释 ID 不会在用户控制之外更改来实现。一个注释可能被另一个用户删除和添加并且具有相同的 ID,这很好。

您将首先收听当前登录的用户user/${auth.id},然后通过添加this.bindAsObject(ref, `note_${noteKeySnapshot.key()}`) 来填充注释。然后每次状态更新:

 render () {
   let noteKeys = Object.keys(this.state)
   noteKeys = noteKeys.filter(k => k.search('note_') === 0)
   let notes = []
   for (let k in noteKeys) {
     notes.push(this.state[k])
   }
   return <div>
     {notes.map(n => <span>{note.created_at}</span>)}  // and so on
   </div>
}

【讨论】:

  • 谢谢,@arve0!据我所知,我将为树中的每个音符创建一个对象,而不是完整的 notes 树本身。对吗?
  • 是的。只需订阅您正在渲染的笔记,属于登录用户的笔记。当然这里缺少一些逻辑,但我相信你能找到它。例如,当添加/删除注释时,您需要订阅/取消订阅。
  • 好的,太好了。现在这更有意义了!我可以使用bindAsObject 将注释对象设置为状态,但它们被设置在状态树的顶部(不确定这是否是正确的术语),因此它们可在this.state.[noteObject] 获得。有没有办法使用 ReactFire 将笔记对象添加到 notes 数组中?以 `this.state.notes.[noteObject]. 结尾会很棒
  • 不确定是否可以在 ReactFire 中定义上下文。在re-base 你可以。它会是这样的:base.syncState({context: this.state.notes, state: noteKey })
  • 这在概念上是有道理的。但是当我尝试context: this.state.notes 时出现错误:“选项参数必须包含对象类型的上下文属性。相反,得到”是否需要添加到状态子元素的另一个步骤?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 2018-11-15
  • 2022-01-12
  • 2014-02-25
相关资源
最近更新 更多