简体   繁体   中英

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:

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. Document references must have an even number of segments, but Users has 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. (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)
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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