【发布时间】:2019-07-17 18:21:48
【问题描述】:
我在使用以下数据库规则从客户端(我的 iOS 应用程序)上的数据库读取和写入数据时遇到问题:
// Checks auth uid equals database node uid
// In other words, the User can only access their own data
{
"rules": {
"posts": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
但是,当使用以下规则时,我在读取和写入数据时没有问题:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
我的目标是让每个用户只能读取/写入自己的数据。任何建议/帮助将不胜感激。
编辑:
尝试发帖时,我使用以下 (iOS):
let key = ref?.childByAutoId().key
let post = ["uid": key,
"title": titleField.text,
"description": descField.text]
ref?.child(key!).setValue(post)
当我想检索这些数据条目时,目前我正试图通过查看参考点(“任务”)来检索数据条目(在 iOS 中,我的数据库参考如下):
ref = Database.database().reference().child("task")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject]
else {
return
【问题讨论】:
-
请编辑问题以显示未按照您期望的方式使用这些规则的特定查询。规则和查询总是在一起的。您的查询可能不符合您的规则。
-
@DougStevenson 我对使用数据库还很陌生,所以我将假设“查询”是指我的数据结构如何。我将使用我的数据结构示例来编辑操作
-
通过查询,他表示您正在使用(尝试)读取/写入数据库的代码。
-
@AndréKool 谢谢,刚刚更新了我如何读/写数据的操作
标签: firebase firebase-realtime-database firebase-security