简体   繁体   English

如何从存储在 Firestore (Swift) 中的文档返回对象

[英]How do I return an object from a document stored in Firestore (Swift)

I have a users collection in firebase and a user struct.我在 firebase 中有一个用户集合和一个用户结构。 I need to write a function that takes in the user's id and returns the corresponding user object:我需要编写一个函数来接收用户的 id 并返回相应的用户对象:

struct AppUser: Codable {

    var id: String
    var displayName: String
    var photoURL: String
    var points: Int?
    var knownLanguageCodes: Set<String>?

}

This is my function that I have so far.这是我迄今为止的功能。

func getUser(id: String) -> AppUser? {

    let db = Firestore.firestore()
    let userRef = db.collection("users").document(id)

    userRef.getDocument { (document, error) in
         if let document = document, document.exists {
            let userID = document.data()?["id"] as! String
            let userDisplayName = document.data()?["displayName"] as! String
            let userPhotoURL = document.data()?["photoURL"] as! String
            let userPoints = document.data()?["points"] as! Int?
            let userKnownLanguageCodes = document.data()?["knownLanguageCode"] as! Set<String>?
            let user = AppUser(id: userID,
                               displayName: userDisplayName,
                               photoURL: userPhotoURL,
                               points: userPoints,
                               knownLanguageCodes: userKnownLanguageCodes)
            return user
         } else {
            print("Error getting user")
            return nil
        }
    }
}

Both of the return statements above give the error: Unexpected non-void return value in void function上面的两个返回语句都给出了错误:在 void 函数中出现意外的非空返回值

I have looked at the code here https://cloud.google.com/firestore/docs/query-data/get-data under the heading 'Custom objects' and it doesn't seem to work for me.我已经查看了https://cloud.google.com/firestore/docs/query-data/get-data标题下的“自定义对象”代码,它似乎对我不起作用。 I get the error: Value of type 'NSObject' has no member 'data'.我收到错误:“NSObject”类型的值没有成员“数据”。 This is produced on line 6 of the code in the link.这是在链接中代码的第 6 行生成的。

You can't return inside a closure use a completion like你不能在闭包内返回使用像这样的完成

func getUser(id: String,completion:@escaping((AppUser?) -> ())) {

    let db = Firestore.firestore()
    let userRef = db.collection("users").document(id) 
    userRef.getDocument { (document, error) in
         if let document = document, document.exists {
            let userID = document.data()?["id"] as! String
            let userDisplayName = document.data()?["displayName"] as! String
            let userPhotoURL = document.data()?["photoURL"] as! String
            let userPoints = document.data()?["points"] as! Int?
            let userKnownLanguageCodes = document.data()?["knownLanguageCode"] as! Set<String>?
            let user = AppUser(id: userID,
                               displayName: userDisplayName,
                               photoURL: userPhotoURL,
                               points: userPoints,
                               knownLanguageCodes: userKnownLanguageCodes)
            completion(user)
         } else {
            print("Error getting user")
            completion(nil)
        }
    }
}

Call称呼

getUser(id:<#str#>) { user in
  print(user)
}

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

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