![](/img/trans.png)
[英]Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but
[英]Document references must have an even number of segments error on a collection reference
我收到了一个相当简单的错误:
Document references must have an even number of segments.
我知道它告诉我什么以及如何修复它,但是它在集合引用上给了我这个错误。
CollectionReference collectionReference = getFirebaseInstance()
.collection(Constants.USERS)
.document(userId)
.collection(Constants.CONTACTS);
在我的数据库中,路径是 (COLLECTION) < DOCUMENTS > (COLLECTION) < DOCUMENTS > 我试图在 users/{id}/contacts 获取用户的所有联系人,但它只是抛出这个错误,有什么想法吗?
getFirebaseInstance 是我创建的一种方法,用于始终获取当前
FirebaseFirestore.getInstance();
您的userId
变量可能包含斜杠。 文档ID不能有斜杠,因为它们在形成文档的“路径”时被视为集合和文档之间的分隔符。
字符串也可能是空的,这是无效的。
因为上述和其他类似的帖子都没有帮助我..
我在调用set数据时遇到此错误,然后在.getDocument(...)之后立即(即读取数据)。 当我在setData方法的完成处理程序中调用我的读取数据方法时,错误消息消失了。
我发现了同样的问题,这就是解决我的问题的方法:
我尝试使用以下方法设置文档:
setDoc()
不提供文档 ID。
所以我用:
addDoc()
所以 firebase 本身为文档提供了一个 ID。
lecon 是:使用 set 你必须提供一个 ID,使用 add 你没有提供 ID firebase 为你做。
谢谢 !
我不确定你的情况,但是,如果你有这样的 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.