对于有效的文档参考,DocumentSnapshot始终为null [英] DocumentSnapshot is always null for a valid document reference

查看:94
本文介绍了对于有效的文档参考,DocumentSnapshot始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Firestore上存储结构化数据,我想获取特定嵌套文档的数据,并且该数据必须是实时的,因为数据是实时更新的.

I'm storing structured data on Firestore and I want to get the data of a specific nested document and it must be in real time because the data is updating in real time.

这是Firestore中的结构:

Here is the structure in Firestore:

所以基本上,我有以下路径:

So basically, I have the following path :

collection(Users).document(23).collection(orders).document(16)
or
/Users/23/orders/21

..并且我想获取订单"集合的文档16的数据.

..and I want to get the data of document 16 of the "orders" collection.

这是代码

  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");


But every time, the null log appear Log.e(">>>>>>>> error ", " documentSnapshot = null");

好吧,在打印异常之后,这似乎是一个权限错误!

well, after printing the exception it seems to be a permission error!

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我实际上是经过身份验证的用户!!?

推荐答案

如果documentSnapshotnull,并且您确定文档存在,则e将具有值.打印它以查看问题所在,通常是与权限有关.

If documentSnapshot is null and you're sure the document exists, then e will have a value. Print it to see what the problem is, which typically will be something about permissions.

从您的更新看来,您的规则似乎只需要一个经过身份验证的用户,但是服务器会根据这些规则拒绝您的侦听器.这意味着您没有登录,或者至少当您尝试从Firestore读取时未登录.

From your update it seems your rules requires just an authenticated user, but your listener is rejected by the server based on those rules. This means you're not signed in, or at least not signed in when you try to read from Firestore.

鉴于您共享的代码未显示任何有关身份验证的信息,因此很难多说.不过,如果您想进行验证,请在使用Log.i("User", FirebaseAuth.getInstance().getCurrentUser())之类的名称调用addSnapshotListener之前记录当前用户.

Given that the code you shared shows nothing about authentication, it's hard to say more. If you'd like to verify though, log the current user just before you call addSnapshotListener with something like Log.i("User", FirebaseAuth.getInstance().getCurrentUser()).

这篇关于对于有效的文档参考,DocumentSnapshot始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆