我正在Firestore上存储结构化数据,我想获取特定嵌套文档的数据,并且该数据必须是实时的,因为数据是实时更新的。
这是Firestore中的结构:
所以基本上,我有以下路径:
collection(Users).document(23).collection(orders).document(16)
or
/Users/23/orders/21
..我想获取“订单”集合的文档16的数据。
这是代码
class FireStoreMapActivity{
DocumentReference doc;
void getData(){
doc = FirebaseFirestore.getInstance()
.collection("Users")
.document(23 + "")
.collection("orders")
.document(21 + "");
doc.addSnapshotListener(this, (documentSnapshot, e) -> {
if (documentSnapshot != null) {
if (documentSnapshot.exists()) {
List<String> images = (List<String>) documentSnapshot.get("images");
Log.e(">>>>>>>>>>> size => ", locHistory.size() + " ");
} else {
Log.e(">>>>>>>> error ", " documentSnapshot != exists");
}
} else {
Log.e(">>>>>>>> error ", " documentSnapshot = null");
}
});
}
}
但是每次都会出现空日志
Log.e(">>>>>>>> error ", " documentSnapshot = null");
好吧,在打印异常后,这似乎是一个权限错误!
com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
这是我的规则集
service cloud.firestore {
match /databases/{database}/documents {
match /Users/{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
但是如何通知Firestore我实际上是经过身份验证的用户!!?
答案 0 :(得分:1)
如果<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div class="jumbotron">
<div id="collapse" style="display:none">
<hr>
<a class="hover-regular" href="https://mau.se/">
<h5>Test</h5></a> <i>2018-</i><br>
<span class="badge badge-pill badge-primary">Test</span>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
</div>
<div class="d-flex justify-content-center">
<a class="nav-toggle hover-regular" href="#collapse">View All</a>
</div>
</div>
是documentSnapshot
,并且您确定文档存在,那么null
将具有一个值。打印它以查看问题所在,通常是与权限有关。
从您的更新来看,您的规则似乎只需要经过身份验证的用户,但是服务器会根据这些规则拒绝您的侦听器。这意味着您未登录,或者至少在尝试从Firestore读取时未登录。
鉴于您共享的代码没有显示任何有关身份验证的信息,因此很难多说。不过,如果您想验证,请在致电e
之类的addSnapshotListener
之类的用户之前登录。