简体   繁体   English

集合引用无效。 集合引用必须有奇数个段

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

I want to get all documents from a sub-collection.我想从子集合中获取所有文档。

Here's the path:这是路径:

Collection(Shops)-Doc(shop)-Collection(year)-Doc(month)-Col(day)-Doc(Current)-Col(initial stocks). Collection(Shops)-Doc(shop)-Collection(year)-Doc(month)-Col(day)-Doc(Current)-Col(initial stocks)。

This is how it looks like in firestore:这就是它在 Firestore 中的样子:

Numbers - Dhool - 2021 - 10(month) - 06(day) - Begining - initial stock数字 - Dhool - 2021 - 10(月) - 06(天) - 开始 - 初始库存

But I'm getting the following error:但我收到以下错误:

错误截图

I know there are other questions like that but they are not solving my problems.我知道还有其他类似的问题,但它们并没有解决我的问题。

Here is the code of the fragment where the query is:这是查询所在的片段的代码:

public class DebutFragment extends Fragment {

Calendar calendar;
DatePickerDialog.OnDateSetListener date;
TextView dateView;

String day="", month="", year="";

public DebutFragment() {}

public static DebutFragment newInstance() {
    return new DebutFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_actuel, container, false);
    dateView = view.findViewById(R.id.date);

    this.configureDate();
    day = getDay(choosenDate);
    month = getMonth(choosenDate);
    year = getYear(choosenDate);

    this.configureRecyclerView(view);

    return view;
}

/////////////
//////////
private void configureDate() {
    calendar = Calendar.getInstance();
    date = (view, year, month, dayOfMonth) -> {
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        updateLabel();
    };

    updateLabel();

    dateView.setOnClickListener(v -> new DatePickerDialog(getContext(), date,
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show());
}

private void updateLabel() {
    SimpleDateFormat dateFormatAbreger = new SimpleDateFormat("E dd MMM yy", Locale.FRANCE);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(BIRTHDAY_FORMAT, Locale.FRANCE);
    dateView.setText(dateFormatAbreger.format(calendar.getTime()));
    choosenDate = simpleDateFormat.format(calendar.getTime());
}

//////////////////
private void configureRecyclerView(View view) {
    RecyclerView recyclerView = view.findViewById(R.id.khat_debut_recyclerview);
    KhatDebutAdapter adapter = new KhatDebutAdapter(options);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    adapter.notifyDataSetChanged();
}

Query query = getKhatInitialeCollectionReference(day, month, year).orderBy("typ", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Khat> options = new FirestoreRecyclerOptions.Builder<Khat>()
        .setQuery(query, Khat.class).setLifecycleOwner(this).build();
  }

Here is the code of all my Firebase methods:这是我所有 Firebase 方法的代码:

public class FirebaseCalls {

//BOSSES
public static CollectionReference getBossesCollectionReference() {
    return FirebaseFirestore.getInstance().collection(PATRONS);
}

public static DocumentReference getBossDocumentReference(String emailBoss) {
    return getBossesCollectionReference().document(emailBoss);
}

//NUMBERS
public static CollectionReference getNumbersBigCollectionReference() {
    return FirebaseFirestore.getInstance().collection(NUMBERS);
}

public static CollectionReference getNumbersCollectionReference() {
    return getBossDocumentReference(currentBoss.getEmail()).collection(NUMBERS);
}

public static DocumentReference getNumberDocumentReference(String name) {
    return getNumbersCollectionReference().document(name);
}

public static DocumentReference getNumberBigDocumentReference() {
    return getNumbersBigCollectionReference().document(currentNumber.getName());
}

///YEAR
public static CollectionReference getYearCollectionReference(String year) {
    return getNumberBigDocumentReference().collection(year);
}

///MONTH
public static DocumentReference getMonthDocumentReference(String month, String year) {
    return getYearCollectionReference(year).document(month);
}

public static CollectionReference getDayCollectionReference(String day, String month, String year) {
    return getMonthDocumentReference(month, year).collection(day);
}

///COMMENCEMENT
public static DocumentReference getCommencementDocumentReference(String day, String month, String year) {
    return getDayCollectionReference(day, month, year).document(COMMENCEMENT);
}

///RESTANT
public static DocumentReference getRestantDocumentReference(String day, String month, String year) {
    return getDayCollectionReference(day, month, year).document(RESTANT);
}

Can somebody help me, please?有人可以帮我吗?

This issue, as you mentioned, is already pointed out in different questions, and it's related to the Firestore data model. Here you can find detailed information about how this data model is structured.正如您提到的,这个问题已经在不同的问题中指出,它与 Firestore 数据 model 有关。在这里您可以找到有关此数据 model 的结构的详细信息。 Basically, the error says you can't have an odd number of segments to refer to a collection;基本上,该错误表明您不能使用奇数个段来引用集合; this is because collections are always going to be in an odd position due to how hierarchy structures are designed.这是因为由于层次结构的设计方式,collections 总是会出现在奇数 position 中。 The documentation says:文件说:

Notice the alternating pattern of collections and documents.注意 collections 和文档的交替模式。 Your collections and documents must always follow this pattern.您的 collections 和文件必须始终遵循此模式。 You cannot reference a collection in a collection or a document in a document.您不能在集合中引用集合或在文档中引用文档。

You can see the pattern here:你可以在这里看到模式:

Firestore 数据结构

And here is an example used in the documentation implemented in a Chat Application to explain Hierarchy structure:这是在聊天应用程序中实现的文档中使用的示例,用于解释层次结构:

To understand how hierarchical data structures work in Cloud Firestore, consider an example chat app with messages and chat rooms.要了解分层数据结构在 Cloud Firestore 中的工作原理,请考虑一个包含消息和聊天室的示例聊天应用。 You can create a collection called “rooms” to store different chat rooms:您可以创建一个名为“rooms”的集合来存储不同的聊天室:

聊天应用参考

And this is how it's referenced:这就是它的引用方式:

 DocumentReference messageRef = db.collection("rooms").document("roomA").collection("messages").document("message1");

This, compared to your application, will be:与您的申请相比,这将是:

Numbers - Dhool - 2021(year) - 10(month) - 06(day) - Beginning - initial stock数字 - Dhool - 2021(年)- 10(月)- 06(日)- 开始 - 初始库存

  • Collection: Numbers收藏:数字
  • Document: Dhool文档:Dhool
  • Subcollection: 2021(year)子集:2021(年)
  • Document: 10(month)文件:10(月)
  • Subcollection: 06(day)子集:06(天)
  • Document: Beginning文件:开始
  • Subcollection: Initial Stock子集合:初始库存

And should be referenced in this way:并且应该以这种方式引用:

CollectionReference NumbersColRef = db.Collection(“Numbers”);
DocumentReference DhoolDocRef = NumbersColRef().Document(“Dhool”);
CollectionReference YearColRef = DhoolDocRef().Collection(“Year”);
DocumentReference MonthDocRef = YearColRef().Document(“Month”);
CollectionReference DayColRef = MonthDocRef().Collection(“Day”);
DocumentReference BeginningDocRef = DayColRef().Document(“Beginning”);
CollectionReference IStockColRef = BeginningDocRef().Collection(“IStock”);

If your structure is already like this, which I can see it is, the error may also occur when the references are not properly called or if one of them is empty or returning an empty or incorrect format value, which might be the case.如果您的结构已经像这样,我可以看到,当引用未正确调用或者其中一个为空或返回空或不正确的格式值时,也可能会发生错误,这可能是这种情况。 You can troubleshoot your parameters and see if they are correctly called and also check the entity IDs if they return a type of value format that might cause a problem.您可以对参数进行故障排除并查看它们是否被正确调用,还可以检查实体 ID 是否返回可能导致问题的值格式类型。 The dates may contain a “/” or another symbol that could cause the program to crash, so be sure to parse it to Strings or Integers, or that the values are not being received in a format that could cause this issue.日期可能包含“/”或其他可能导致程序崩溃的符号,因此请务必将其解析为字符串或整数,或者未以可能导致此问题的格式接收值。 This was found in another forum with the same error, but the cause was a little different from what the error says.这是在另一个同样错误的论坛上找到的,但原因与错误所说的有点不同。 Please take in consideration this is not from the official documentation.请考虑这不是来自官方文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 集合引用必须有奇数个段 - Collection references must have an odd number of segments 文档引用在集合引用上必须有偶数段错误 - Document references must have an even number of segments error on a collection reference Firestore addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但 - Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but 未捕获的 FirebaseError:无效的文档引用。 文档引用必须有偶数个段,但 todos 有 1 - Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1 Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but<x> 有 1,空)</x> - Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but <x> has 1, null) “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" Firebase 文档删除奇数段 - Firebase document deletion odd number of segments 在 firebase 使用子集合参考搜索主集合 - In firebase searching main collection using sub collection reference 如何将客户的收藏限制为 1? - How to limit client to have collection to 1? 如何获取 firebase 集合中的元素个数? - How to get the number of elements in a firebase collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM