我正在Vue JS中建立一个新项目。我正在建立一个与Firebase集成的博客。这个想法是,任何登录的用户都可以创建博客帖子,并将其保存到Firebase实时数据库下:https://xxx.firebaseio.com/blogs.json
,每个博客都有唯一的Firebase ID。
我遇到的问题是,如果有多个用户创建博客文章,传统的Firebase规则规定,对于登录用户,博客文章将在that用户下创建,如果另一个用户登录后,其博客帖子将显示在其用户下。
这很好,但是这意味着当我尝试对我的博客帖子进行v-for
时,我有两个对象,每个对象都为用户服务,每个对象中都可能有多个博客对象,而无法删除这些对象。
以下是我当前的规则:
{
"rules": {
"blogs": {
".read": true,
".write": false,
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
我希望能够为已登录的用户创建所有博客帖子,并在blogs
下而不是在用户ID下创建帖子:$uid
,如何实现?
当前,这是我在列出当前规则时得到的输出:
{
"PvWsi3t9mFZza5gXP8qNQaCktDN2": {
"-LrZ87XS02DanBTybhij": {
"body": "<p><strong>test</strong></p>",
"creation": "",
"slug": "test",
"title": "My very first blog post"
},
"-LrZ8KjsJyGT1YFsIJ3G": {
"body": "<p><strong>jjj</strong></p>",
"creation": "2019-10-19",
"slug": "test-2",
"title": "Second post"
}
},
"yLLOq9jC5odefHBENkO3r2ztNzZ2": {
"-LrZ8ZRhLVEylBFaGcnq": {
"body": "<p>content here</p>",
"creation": "2019-10-04",
"slug": "test-two",
"title": "My very first blog post - ryan"
}
}
}
使用(未测试的)规则>
"rules": { "blogs": { ".read": true, "$blogId": { ".write": "(!data.exists() || data.child('author).val() == auth.uid) && (newData.child('author') == auth.uid || !newData.exists())" } } }
您可以通过在博客中添加
author
字段并将其强制为创建者的UID来为博客使用扁平结构。
第一行“ .write”行的意思是“这是新博客还是您的博客”。
第二行的意思是“现在是您的博客,或者它已被删除”。