文档引用在集合引用上必须具有偶数个段错误



我得到一个相当简单的错误:

Document references must have an even number of segments.

我知道它在告诉我什么以及如何修复它,但是它在集合引用上给了我这个错误。

CollectionReference collectionReference = getFirebaseInstance()
.collection(Constants.USERS)
.document(userId)
.collection(Constants.CONTACTS);

在我的数据库中,路径是(集合(<文档>(集合(<文档>我正在尝试在 users/{id}/contacts上获取用户的所有联系人,但它只是抛出此错误,有什么想法吗?

getFirebaseInstance是我创建的一种方法,用于始终获取当前

FirebaseFirestore.getInstance();

您的userId变量中可能有一个斜杠。 文档 ID 不能有斜杠,因为在形成文档的"路径"时,它们入为集合和文档之间的分隔符。

字符串也可能为空,这是无效的。

我发现了同样的问题,这就是解决我的问题的原因:

我尝试使用以下命令设置文档:

setDoc()

不提供文档 ID。

所以我用了:

addDoc()

因此,Firebase 本身为文档提供了一个 ID。

乐孔是: 使用集合时,您必须提供ID,使用添加时,您不需要提供ID火碱为您做。

谢谢!

因为上述帖子和其他类似帖子都没有帮助我..

我在调用集合数据时出现此错误,然后紧跟在 .getDocument(...( 之后(即读取数据(。当我在 setData 方法的完成处理程序中调用读取数据方法时,错误消息消失了。

我不确定是不是你的情况,但是,如果你有这样的函数,它通过"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 };
})
);

}

CollectionReference collectionReference = getFirebaseInstance()
.collection(Constants.USERS)
.document(userId)
.collection(Constants.CONTACTS);

造成这种错误的原因之一是..当(Constant.USERS,userId,Constants.CONTACTS(中的一个字符串是空字符串("(时。我已经解决了我的错误

最新更新