Firestore文档存在在iOS Swift中返回false结果

时间:2018-01-14 16:28:15

标签: ios swift firebase google-cloud-firestore

我的Firestore数据库中不存在单个文档或集合, 但是当调用这段代码时,它打印出" Document exists == true。用户数据:(user.data())"

libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

错误。

let reference = database.collection("users").document(currentUser.uid)

reference.getDocument { (snapshot, error) in
    guard let user = snapshot else {
        print("Document exist == false")
        return
    }

    print("Document exist == true. User data:  \(user.data())")
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

除了检查是否返回了非空快照之外,您还应该使用exists方法:

let reference = database.collection("users").document(currentUser.uid)

reference.getDocument { (snapshot, error) in

    guard
        let snapshot = snapshot,
        let user = snapshot.exists ? snapshot : nil
    else {
        print("Document exist == false")
        return
    }
    print("Document exist == true. User data:  \(user.data())")
}