Firestore崩溃 [英] Crashes with Firestore

查看:36
本文介绍了Firestore崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查询集合中不存在的文档时,我仍然返回一个非null对象,当我尝试调用 documentSnapshot?.data()时,该对象会崩溃./p>

返回的错误是"Document'< FSTDocumentKey:XXXXXX>'不存在.请在调用document.data之前检查document.exists以确保该文档存在.'"

解决方案

我发现可以用一个漂亮的小警卫声明来解决这个问题:

  guard(documentSnap?.exists ?? false),错误== nil else {return} 

以下是此检查的有效示例:

  func getUserData(uid:字符串,完成:@escaping(([[String:Any] ?, Error?))->()){让document = defaultStore.collection("user-data").document(uid)document.getDocument {(documentSnap,error)在保护(documentSnap?.exists ?? false),错误== nil else {完成((nil,错误))返回}完成((documentSnap?.data(),错误))}} 

我不知道为什么您必须调用 .exists(),文档不仅是nil,而且还是学习曲线的一部分,还是Beta版的产物SDK

When I query a collection for a document that doesn't exist, I'm still returned a non-nil object, which crashes when I try to call documentSnapshot?.data().

The error returned is "Document '<FSTDocumentKey: XXXXXX>' doesn't exist. Check document.exists to make sure the document exists before calling document.data.'"

解决方案

I've found this can be solved with a nifty little guard statement:

guard (documentSnap?.exists ?? false), error == nil else { return }

Here's a working example of this check:

func getUserData(uid: String, completion: @escaping(([String: Any]?, Error?)) -> ()) {

        let document = defaultStore.collection("user-data").document(uid)

        document.getDocument { (documentSnap, error) in

            guard (documentSnap?.exists ?? false), error == nil else {
                completion((nil, error))
                return
            }

            completion((documentSnap?.data(), error))
}}

I don't know why you have to call .exists(), and the document isn't just nil, but this is either part of the learning curve, or an artifact of a Beta SDK

这篇关于Firestore崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆