【发布时间】: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