您将需要一个名为 Books 的不同父节点:-
现在你的 JSON 是这样的:-
posts : {
userID1 : {
BOOK_11_ID : {.....}
};
userID2 : {
BOOK_21_ID : {.....},
BOOK_22_ID : {.....},
BOOK_23_ID : {.....},
BOOK_24_ID : {.....},
}
}
您必须将数据库 JSON 结构修改为:-
posts : {
Users_books :{
userID1 : {
BOOK_11 : true // Value 'true' means that this user
// has subscribed or bought this book or whatever
};
userID2 : {
BOOK_21 : true,
BOOK_22 : true,
BOOK_23 : true,
BOOK_24 : true,
}
},
Books:{
BOOK_11_ID : {/Book details/};
BOOK_21_ID : {/Book details/};
BOOK_22_ID : {/Book details/};
BOOK_23_ID : {/Book details/};
BOOK_24_ID : {/Book details/};
}
}
修改“图书”部分的安全规则,使所有经过身份验证的用户都可以看到。
{
"rules" :{
"Users_books" : {
".write" : "auth != null && auth.uid === $uid", // Will allow only the user to make any change within its node and no-one else
".read": "auth != null && auth.uid === $uid"
},
"Books" : {
".write" : "auth != null", // Will allow all the users that are authenticated to your app to access every books detail.
".read" : "auth != null"
}
}
}
现在你要做的是:-
1) 如果是创建书籍的用户;然后将书籍详细信息存储在books下的父节点'Books'中uid并在 users 节点中将 book_ID 的值设置为 true。
2)如果数据库都是后端的,即你输入的;每当用户订阅或购买一本书时,只需获取该书的 uid 并在该用户的部分将其设置为 true 。
PS:始终扇出您的数据库;从中搜索起来要容易得多。尽管我很确定您在不久的将来会想要存储用户的一些个人详细信息,因此只需创建另一个节点即可。命名它'Users',安全规则将与'用户图书'及以下'Users'父节点附加每个用户的详细信息及其uid作为他们的钥匙。