[英]Document references must have an even number of segments error on a collection reference
I'm getting the rather simple error:我收到了一个相当简单的错误:
Document references must have an even number of segments.
I'm aware of what it is telling me and how to fix it, however it is giving me this error on a collection reference.我知道它告诉我什么以及如何修复它,但是它在集合引用上给了我这个错误。
CollectionReference collectionReference = getFirebaseInstance()
.collection(Constants.USERS)
.document(userId)
.collection(Constants.CONTACTS);
In my database the path is (COLLECTION) < DOCUMENTS > (COLLECTION) < DOCUMENTS > I'm trying to get all contacts for a user at users/{id}/contacts but it just throws this error, any ideas?在我的数据库中,路径是 (COLLECTION) < DOCUMENTS > (COLLECTION) < DOCUMENTS > 我试图在 users/{id}/contacts 获取用户的所有联系人,但它只是抛出这个错误,有什么想法吗?
getFirebaseInstance is a method I've created to always get the current getFirebaseInstance 是我创建的一种方法,用于始终获取当前
FirebaseFirestore.getInstance();
Your userId
variable probably has a slash in it. 您的
userId
变量可能包含斜杠。 Document ids can't have slashes, since they are interepreted as dividers between collections and documents when forming the "path" to a document. 文档ID不能有斜杠,因为它们在形成文档的“路径”时被视为集合和文档之间的分隔符。
It's also possible that the string may be empty, which is invalid. 字符串也可能是空的,这是无效的。
Because neither the above nor other similar posts helped me.. 因为上述和其他类似的帖子都没有帮助我..
I got this error when calling set data and then immediately after .getDocument(...) (ie reading data). 我在调用set数据时遇到此错误,然后在.getDocument(...)之后立即(即读取数据)。 When I called my read data method in the completion handler of the setData method then the error message disappeared.
当我在setData方法的完成处理程序中调用我的读取数据方法时,错误消息消失了。
I found the same problem, this is what solved my problem:我发现了同样的问题,这就是解决我的问题的方法:
I tried to set a document using the:我尝试使用以下方法设置文档:
setDoc()
without providing the document id.不提供文档 ID。
So I used:所以我用:
addDoc()
so firebase itself provide an id for document.所以 firebase 本身为文档提供了一个 ID。
The lecon is that: using set you must provide an ID, using add you do not have provide the ID firebase do it for you. lecon 是:使用 set 你必须提供一个 ID,使用 add 你没有提供 ID firebase 为你做。
Thanks !谢谢 !
i'm not sure that's your case,But, if you have a function like this, that brings data by "Id" from your firebase, just add a slash "/" after your collection's name我不确定你的情况,但是,如果你有这样的 function,它会从你的 firebase 中通过“Id”带来数据,只需在集合名称后添加一个斜杠“/”
getDetailProduct(id: string): Observable<Interface_name> {
const productsDocuments = this.angularFirestore.doc<Interface_name>(
'collection_name/' + id //here after your collection name add "/"
);
return productsDocuments.snapshotChanges().pipe(
map((changes) => {
const data = changes.payload.data() as Interface_name;
const id = changes.payload.id;
return { id, ...data };
})
);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.