[英]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(),
};
})
);
});
}, []);
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.