swift - 从 Firebase 更改为 Firestore 导致查询中出现 fatal error : Unexpectedly found nil . ..

标签 swift firebase google-cloud-firestore

在将数据从 Firebase 迁移到 Firestore 后,我正在尝试从 Firestore 检索用户的数据。下面的代码在 firebase 上运行良好,并检索了用户的数据。 Firebase query

但是,将查询更改为 firestore 查询后,我收到此错误。 FireStore query

此处打印语句“document.data()”包含数据,但我收到此错误。我不知道这个错误是从哪里来的。

当我将 document.data() 与 nil 进行比较时,我得到“文档数据:包含 nil”。 Document data: contains nil

我不知道如何获取数据。

这是我收到错误的代码,

    static func getUser(uid: String, setUserDefaults: @escaping (NormalUser) -> Void){
   DataService.ds.REF_USERS_NORMAL.document(uid).getDocument { (document, error) in
        if error != nil{
            print("\(String(describing: error?.localizedDescription))")
        }else{
                if document != nil{
                    let data = document?.data() as! [String: String]
                   print("Document data: \(String(describing: document?.data() as! [String: String]))")
                    let user = NormalUser(userData: (data as Dictionary<String, AnyObject>))
                    setUserDefaults(user)
                }else{
                    print("Document data: contains nil")
                }
        }
    }
}

这是我定义变量的方式,

 import Foundation
 import Firebase

class NormalUser: User {

private var _email: String?
private var _city: String?
private var _country: String?
private var _name: String?
private var _phone: String?
private var _profileImgUrl: String?


var email: String {
    return _email!
}
var city: String {
    return _city!
}
var country: String {
    return _country!
}
var name: String {
    return _name!
}
var phone: String {
    return _phone!
}
var profileImgUrl: String {
    set{
        self.profileImgUrl = _profileImgUrl!
    }
    get{
        if let pI = _profileImgUrl{
            return pI
        }
        return ""
    }
}

init(userData: Dictionary<String, AnyObject>) {

    super.init(userId: userData["uid"] as! String, user: userData)

    if let email = userData["email"] as? String {
        self._email = email
    }
    if let city = userData["city"] as? String {
        self._city = city
    }
    if let country = userData["country"] as? String {
        self._country = country
    }
    if let name = userData["name"] as? String {
        self._name = name
    }
    if let phone = userData["phone"] as? String {
        self._phone = phone
    }
    if let profileImgUrl = userData["imgUrl"] as? String {
        self._profileImgUrl = profileImgUrl
    }
}

static func createNormalUser(uid: String, userData: Dictionary<String, String>) {
    // add user to database
    //DataService.ds.REF_USERS_NORMAL.child(uid).setValue(userData)
    DataService.ds.REF_USERS_NORMAL.document(uid).setData(userData) { (err) in
            if let err = err {
                print("Error writing document: \(err)")
            } else {
                print("Document successfully written!")
            }
    }

    addUserToGroup(uid:uid, group:"normal")
}

static func updateUserProfile(uid: String, userData: Dictionary<String, String>) {
    //DataService.ds.REF_USERS_NORMAL.child(uid).updateChildValues(userData)
   DataService.ds.REF_USERS_NORMAL.document(uid).updateData(userData)
}

static func getUser(uid: String, setUserDefaults: @escaping (NormalUser) -> Void){

   DataService.ds.REF_USERS_NORMAL.document(uid).getDocument { (document, error) in
        if error != nil{
            print("\(String(describing: error?.localizedDescription))")
        }else{
                if document != nil{
                    let data = document?.data() as! [String: String]
                   print("Document data: \(String(describing: document?.data() as! [String: String]))")
                    let user = NormalUser(userData: (data as Dictionary<String, AnyObject>))
                    setUserDefaults(user)
                }else{
                    print("Document data: contains nil")
                }
        }
    }
}

}

最佳答案

我解决了这个问题!

对于任何可能遇到此错误的人来说,问题出在 userId 上。我从 firebase 获取用户 id,就我而言,它不再为我的查询提供服务,最终得到一个 nil。当我直接从身份验证中获取ID时,

 let userID = Auth.auth().currentUser?.uid

问题解决了!

关于swift - 从 Firebase 更改为 Firestore 导致查询中出现 fatal error : Unexpectedly found nil . ..,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577773/

相关文章:

在 TableView 中使用搜索栏时,Swift fatal error 索引超出范围

ios - 当点击 iOS Swift 中的“下一步”按钮时,如何在 TableView 中加载数据数组?

javascript - Firebase 安全规则 Emberfire 每个节点的多个访问级别

firebase - 错误 : Cloud not find the correct Provider<List<Brew>> above this BrewList Widget

ios - UITableViewCell 忽略自动布局

swift - 如何设置画中画的初始位置? (AVPictureInPictureController/AVPlayerViewController)

android - 在 Android 上使用 Firebase 3.0 进行电子邮件验证

iOS Firebase CrashlyticsKit 未设置用户 ID

angular - 如何从 angularfire2 创建 GeoPoint?

javascript - Firebase .where() .limit() .orderBy() 的新命名空间