Firebase Cloud Firestore:无效的集合参考。集合引用,必须是单数段

问题描述:

的我有以下代码和得到一个错误:Firebase Cloud Firestore:无效的集合参考。集合引用,必须是单数段

Invalid collection reference. Collection references must have an odd number of segments 

和代码:

private void setAdapter() { 
     FirebaseFirestore db = FirebaseFirestore.getInstance(); 
     db.collection("app/users/" + uid + "/notifications").get().addOnCompleteListener(task -> { 
      if (task.isSuccessful()) { 
       for (DocumentSnapshot document : task.getResult()) { 
        Log.d("FragmentNotifications", document.getId() + " => " + document.getData()); 
       } 
      } else { 
       Log.w("FragmentNotifications", "Error getting notifications.", task.getException()); 
      } 
     }); 
    } 

分层数据结构和子集合中the documentation描述。集合包含文档,文档可能包含子集合。结构始终是收集和文件的交替模式。该文档包含此示例的说明:

请注意收集和文档的交替模式。您的 收藏和文件必须始终遵循此模式。您不能 引用集合中的集合或文档中的文档。

因此,一个集合的有效路径将总是有奇数个段;一个文档的有效路径,一个偶数。由于您的代码试图查询集合,因此四个路径的长度无效。

+0

好吧,我需要的是这样的:应用/用户/用户名/通知/ {auto-gen-id}/document(通知对象)。我如何在控制台或代码中创建这种类型的东西? – Relm

+0

@Reim:控制台支持创建你想要的结构。 –

然后,你需要改变这样的:

db.collection("app/users/" + uid + "/notifications")... 

此:

db.collection("app").document("users").collection(uid).document("notifications") 

欢迎你;)