从 Firestore 检索集合时出错

问题描述 投票:0回答:0

我有一个用户集合,每个用户都有一个称为投资组合的属性集合, 我能够读取数据,但是一旦我尝试制作文档的副本,我就会遇到错误“尾随闭包传递给不接受闭包的‘FirestoreSource’类型的参数”。

struct PortfolioView: View {
    
    //    var user: User
    @State var portfolioItems: [Property] = []
    private var db = Firestore.firestore()
    
    
    @State private var viewModel = PortfolioViewModel()
    
    @State private var showingAddScreen = false
    @State var myDic: [Any] = []
    
    @EnvironmentObject var portfolioListViewModel: PortfolioViewModel
    
    
    var body: some View {
        NavigationView {
            List(portfolioItems) { property in
                PropertyCell(property: property)
            }
            .onAppear {
                getPortfolioItems()
            }
        }
    }


    private func getPortfolioItems() {    
        if let currentUser = Auth.auth().currentUser {
            let userID = currentUser.uid
            print("User ID: \(userID)")
            
            
            // assuming you have a reference to the Firestore database
            
            // assuming you have the user's ID stored in a variable called "userID"
            let userRef = db.collection("users").document(userID)
            
            // access the "portfolio" subcollection for the user
            let portfolioCollection = userRef.collection("portfolio")
            
            // create an empty array to store the posrtfolio properties
            var portfolioArray = [Property]()
        
            portfolioCollection.getDocuments() { (querySnapshot, err) in
                if let err = err {
                    print("Error getting documents: \(err)")
                } else {
                    for document in querySnapshot!.documents {
                        let dictionary =  document.data()
                        //print("\(document.documentID) => \(document.data())")
                        print(dictionary["property_address"]!)
                        myDic.append(dictionary)
                        print(myDic.count)
            
                    }
                }
                myDic.forEach({ (dictionary) in
                    print(dictionary)
                    let property = Property(address:dictionary["property_address"],
                                           bath: dictionary["property_baths"],
                                           room: dictionary["property_beds"],
                                           size: dictionary["property_price"],
                                           price: dictionary["property_size"])

                })
                
            }
    }

我试图使文档的范围超出范围,但我遇到了一个错误“‘Any’类型的值没有下标”

ios swift firebase google-cloud-firestore swiftui
© www.soinside.com 2019 - 2024. All rights reserved.