无法通过ID读取特定文档的数据

时间:2020-03-14 23:57:19

标签: swift google-cloud-firestore

我有此代码:

Auth.auth().signIn(withEmail: email, password: password) { (authResult, err) in

self.logInButton.isLoading = false

// Check for errors
if err != nil {

// There was an error login to the system
self.showError("Incorrect login or password")
} else {
    let db = Firestore.firestore()

    guard let uid = Auth.auth().currentUser?.uid else { return }

    // Here the program don't enter
    // In this case, complete shows the crossed out getDocument function
    db.collection("users").document(uid).getDocument { (shapshot, error) in
    guard let snap = shapshot, error == nil else {
        self.showError(error!.localizedDescription)
        return
    }
    ...
}

尽管一切都存在,但我无法读取数据。当程序涉及到getDocument时,它就不会进入该程序。我做错了什么?

已更新。 也许控制台输出会以某种方式帮助您:

2020-03-15 07:35:37.919683+0500 SmartLibrary[1956:39141] 6.19.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60301000 started
2020-03-15 07:35:37.986973+0500 SmartLibrary[1956:39141] 6.19.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see ...)
2020-03-15 07:35:37.992944+0500 SmartLibrary[1956:39141] 6.19.0 - [Firebase/Analytics][I-ACS031025] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
2020-03-15 07:35:38.412739+0500 SmartLibrary[1956:39185] 6.19.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-03-15 07:35:38.809486+0500 SmartLibrary[1956:39148] 6.19.0 - [Firebase/Analytics][I-ACS800003] Registered an SDK that has already registered. Default flags will be overwritten. SDK name: app_measurement
2020-03-15 07:35:38.811448+0500 SmartLibrary[1956:39148] 6.19.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-03-15 07:35:39.585977+0500 SmartLibrary[1956:39134] 6.19.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled

1 个答案:

答案 0 :(得分:0)

从结果中获取用户ID:

Auth.auth().signIn(withEmail: email, password: password) { (authResult, err) in

    if let uid = authResult?.user.uid { // successfully signed in

        Firestore.firestore().collection("users").document(uid).getDocument { (shapshot, error) in

            if let snapshot = snapshot {
                ...
            } else { // error with database

                if let error = error {
                    print(error) // print to console, find out why
                }

            }

        }

    } else { // error with login

        if let err = err {
            print(err) // print to console, find out why
        }

    }

}

如果仍然无法执行,则可能是凭据问题,或者使用了错误的电子邮件或密码,或者管理员本身的用户不存在或被禁用。

在编写代码进行即时调试时,养成打印到控制台的习惯。