gpt4 book ai didi

Document references must have an even number of segments error on a collection reference(文档引用在集合引用上必须有偶数段错误)

转载 作者:bug小助手 更新时间:2023-10-24 17:55:07 26 4
gpt4 key购买 nike



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?

在我的数据库中,路径是(集合)<文档>(集合)<文档>我试图获取用户在USERS/{id}/Contents中的所有联系人,但它只是抛出这个错误,您有什么想法吗?



getFirebaseInstance is a method I've created to always get the current

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



FirebaseFirestore.getInstance();

更多回答

Ok I think this maybe to do with this userId variable I'll test and post back

好的,我想这可能与这个用户ID变量有关,我将测试并回发

Use slash way like "users/userid/contacts"

使用斜杠方式,如“USERS/USERID/CONTACTS”

优秀答案推荐

Your userId variable probably has a slash in it. Document ids can't have slashes, since they are interepreted as dividers between collections and documents when forming the "path" to a document.

您的用户ID变量中可能有一个斜杠。文档ID不能有斜杠,因为在形成文档的“路径”时,它们被解释为集合和文档之间的分隔符。



It's also possible that the string may be empty, which is invalid.

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



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.

教训是:使用set你必须提供一个ID,使用Add你没有提供ID Firebase为你做它。


Thanks !

谢啦!



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

One of the reason for this kind of error is.. when one of the string out of (Constant.USERS, userId, Constants.CONTACTS) is empty string (""). I have resolved my error by this

造成这种错误的原因之一是..当(Constant.USERS,UserID,Constants.CONTACTS)中的一个字符串为空字符串(“”)时。我用这句话解决了我的错误



Because neither the above nor other similar posts helped me..

因为上面的帖子和其他类似的帖子都帮不了我。



I got this error when calling set data and then immediately after .getDocument(...) (i.e. reading data). When I called my read data method in the completion handler of the setData method then the error message disappeared.

在调用set data,然后紧跟在.getDocument(...)之后时,我收到了这个错误(即读取数据)。当我在setData方法的完成处理程序中调用我的Read Data方法时,错误消息消失了。



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

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

}

}


更多回答

thats a good shout but unfortunately not the userId is a firebase auto generated id heres an example 37yntLaKSGPgNVrRs18M9GDRLy42

这是一个很好的答案,但不幸的是,用户ID不是Firebase自动生成的ID,下面是一个示例37yntLaKSGPgNVrRs18M9GDRLy42

So are you absolutely certain the error message is happening with the code you're showing? Please show all the relevant code and logs that demonstrate the values of everything at the time of the error.

那么,您是否完全确定错误消息是与您所显示的代码一起出现的?请显示所有相关代码和日志,以显示错误发生时所有内容的值。

Yes this is the exact piece of code that's having an issue it doesn't crash on first run but if I leave my app and come back to it later it crashes also if I copy and paste it somewhere earlier In My code it will crash with the same error and point me to here, it seems like the user id string I'm using (its a shared preference) is being cleared at some point apparently this can happen when two threads try to access it so I'll swap this to get the string from a database and if the issue persists I'll update

是的,这正是一段有问题的代码,它不会在第一次运行时崩溃,但如果我离开我的应用程序并稍后返回它,它也会崩溃如果我将它复制并粘贴到我的代码中之前的某个位置,它将崩溃并返回相同的错误并指向此处,似乎我正在使用的用户ID字符串(这是共享的首选项)在某个时候被清除了显然当两个线程试图访问它时,这可能会发生,所以我会交换它来从数据库中获取字符串,如果问题仍然存在,我会更新

this seems to be fixed and was caused by the userId string being empty but i'm going to mark your answer as correct as its the closest to it and others finding this will know that it must be the variable here many thanks

这似乎是修复的,是由用户ID字符串为空引起的,但我会将您的答案标记为正确,因为它最接近它,其他找到它的人会知道它一定是这里的变量,谢谢

Edited the answer. I also filed a bug with the Firestore client team asking for a more helpful error message in the event that a collection or document id is empty.

编辑了答案。我还向FiRestore客户端团队提交了一个Bug,要求在集合或文档ID为空的情况下提供更有用的错误消息。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com