【问题标题】:Firebase Realtime Database - How to subscribe to database additions?Firebase 实时数据库 - 如何订阅数据库添加?
【发布时间】:2019-10-02 20:49:53
【问题描述】:

我的 Firebase 实时数据库中有一组笔记。 我的客户订阅了数据库的 /notes 路径中的修改。 但是,当“客户端 A”添加新注释时,数据不会推送到“客户端 B”。

export const startSubscribeNotes = () => {
  return (dispatch, getState) => {
    const uid = getState().auth.uid

    database.ref('notes')
    .orderByChild(`access/members/${uid}`)
    .equalTo(true)
    .on('value', (snapshot) => {
      console.log('notes .on()')
      let updatednotes = []
      snapshot.forEach( (data) => {
        const note = {
          id: data.key,
          ...data.val()
        }
        updatednotes.push(note)
      })
      dispatch(setnotes(notes))
    })
  }
}

我的数据库结构

我的 Firebase 访问规则

"notes": {
  ".indexOn": ["data/title", "access/author"],      
  //entry-level access
  ".read": "
    auth.uid !== null && query.equalTo === auth.uid
  ",
  "$note_id": {
    ".write": "
      //If new data
      (!data.exists() && auth.uid !== null) ||
      (
        data.child('access').child('author').val() === auth.uid 
      ||
        data.child('access/members').child(auth.uid).exists()
      )
    ",
    "data": {
      //access
      ".read": "
        //if author or assigned user
        data.parent().child('access').child('author').val() === auth.uid ||
        data.parent().child('access/members').child(auth.uid).exists()
      "
    }
  }
}

上面的访问规则是为了防止客户阅读所有的“笔记”。

但是 - 由“客户 A”创建的“客户 B”有权访问的新便笺永远不会被推送到“客户 B”。如果“客户端 B”重新加载应用程序,则会出现新的注释——但这违背了订阅的目的。

如何确保添加到 /notes 节点的内容被推送到我的客户端并设置了访问规则?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database firebase-security


    【解决方案1】:

    上面的代码有效。问题出在dispatch(setnotes(notes))。 我不小心做了一些额外的检查,没有将所有笔记都传递给数组。 :/ 因此我向商店发送了错误的数据。发送正确的updatednotes 解决了这个问题。 /K

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多