简体   繁体   English

无效的文档参考。 文档引用必须有偶数个段,但用户有 1'

[英]Invalid document reference. Document references must have an even number of segments, but Users has 1'

I'm using Firestore with SwiftUI and I try to make a simple query to get documents from a sub-collection:我正在将 Firestore 与 SwiftUI 一起使用,并尝试进行一个简单的查询以从子集合中获取文档:

struct MyCardsView: View {
    
    @State var myCards = [MyCard]()
    
    @State var show: Bool = false
    @State var selectedId: String = ""
    
    var body: some View {
        
        VStack {
            
            List(self.myCards){i in
                
                Button(action: {
                    
                    self.selectedId = i.card_id
                    self.show.toggle()
                    
                    
                }) {
                    
                    MyCardCell(cardId: i.card_id)
                }
                
            }
            
        }
        .onAppear {
            let db = Firestore.firestore()
            if let uid = Auth.auth().currentUser?.uid {
                db.collection("Users").document(uid).collection("Fiches").order(by: "date", descending: true).addSnapshotListener { (snap, err) in
                    
                    guard let documents = snap?.documents else {
                        print("No documents")
                        return
                    }
                    
                    self.myCards = documents.compactMap({ (queryDocumentSnapshot) -> MyCard? in
                        return try? queryDocumentSnapshot.data(as: MyCard.self)
                    })
                    
                }
            }
        }
        .sheet(isPresented: self.$show){
            CardsView(cardId: self.selectedId)
        }
        
    }
}

This view has always worked, but since yesterday I get the following very strange error:这种观点一直有效,但从昨天开始,我收到以下非常奇怪的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference.由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“FIRESTORE INTERNAL ASSERTION FAILED:文档引用无效。 Document references must have an even number of segments, but Users has 1'文档引用必须有偶数个段,但用户有 1'

I really don't understand because the path is correct and the sub-collection exists...我真的不明白,因为路径是正确的,并且子集合存在......

Im having this same issue, but only in my canvas previews.我有同样的问题,但只在我的画布预览中。 The app runs fine.该应用程序运行良好。 I've looked through my code and have figured it to come down to this block of code, because theres only one reference to the collection and not to a document ID, however on the Firebase documentation, it says that this code is good even though it only has one segment.我已经查看了我的代码并认为它​​可以归结为这个代码块,因为只有一个对集合的引用而不是对文档 ID 的引用,但是在 Firebase 文档中,它说这段代码很好,即使它只有一个段。 (although I'm just taking a guess) (虽然我只是猜测)

public func fetchAllArtists(completion: @escaping ([Artist]) -> Void) {
    var _artists: [Artist] = []
    database
        .collection(ContainerNames.artists)
        .getDocuments { snapshot, error in\
            guard error == nil else { return }
            for document in snapshot!.documents {
                // do stuff
            completion(_artists)
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM