简体   繁体   English

未捕获的 FirebaseError:无效的文档引用。 文档引用必须有偶数个段,但 todos 有 1

[英]Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1

I'm getting the error: Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1.我收到错误: Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1. Uncaught FirebaseError: Invalid document reference. Document references must have an even number of segments, but todos has 1.

I wrote code because I want to inquire the data in the DB.我写代码是因为我想查询数据库中的数据。

No matter how much I searched, I couldn't find the answer.无论我搜索多少,我都找不到答案。 which part is wrong哪一部分是错误的

this is my firebaseConfig code:这是我的 firebaseConfig 代码:

import firebase from "firebase/compat/app";
import "firebase/compat/firestore";
import "firebase/compat/auth";

const firebaseConfig = {
  apiKey: "***",
  authDomain: "***",
  projectId: "***",
  storageBucket: "***",
  messagingSenderId: "***",
  appId: "***",
  measurementId: "***",
};

firebase.initializeApp(firebaseConfig);

const firestore = firebase.firestore();

export { firestore };

export const auth = firebase.auth();

export const apiKey = firebaseConfig.apiKey;

this is code of firebaseController.tsx:这是 firebaseController.tsx 的代码:

export const todos = firestore.collection('todos');

and this is code of view component:这是视图组件的代码:

useEffect(() => {
    onSnapshot(todos, (snapshot: QuerySnapshot<DocumentData>) => {
      setTodoItems(
        snapshot.docs &&
          snapshot.docs.map((doc) => {
            return {
              id: doc.id,
              ...doc.data(),
            };
          })
      );
    });
  }, []);

my database: DB我的数据库: DB

The onSnapshot method works on Firebase Documents, not on Firebase Collections. The first argument to onSnapshot you give ie "todos" is a collection reference but in reality, it expects a document reference, which you obtain by using the method onSnapshot 方法适用于 Firebase 文档,而不适用于 Firebase Collections。您给 onSnapshot 的第一个参数,即“todos”是一个集合引用,但实际上,它需要一个文档引用,您可以使用该方法获得

const docRef = doc(db, "path/to/document/inside/todos/collection")
onSnapshot(docRef, ......)

Hope this helps!希望这可以帮助!

暂无
暂无

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

相关问题 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 addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但 - Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" 文档引用在集合引用上必须有偶数段错误 - Document references must have an even number of segments error on a collection reference 集合引用无效。 集合引用必须有奇数个段 - Invalid collection reference. Collection references must have an odd number of segments 未捕获(承诺)FirebaseError:无效文档引用 - Uncaught (in promise) FirebaseError: Invalid document reference updateDoc:未捕获的 FirebaseError:无效的文档引用 - updateDoc: Uncaught FirebaseError: Invalid document reference 集合引用必须有奇数个段 - Collection references must have an odd number of segments Firestore 错误:文档必须具有偶数个路径元素 - Firestore error: A document must have an even number of path elements 未捕获(承诺)FirebaseError:没有要更新的文档。 如果我对文档 ID 进行硬编码,则只能更新文档 - Uncaught (in promise) FirebaseError: No document to update. Can only update document if I hard code the document id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM