从 expo 环境调用后 Firestore 返回元数据

Posted

技术标签:

【中文标题】从 expo 环境调用后 Firestore 返回元数据【英文标题】:Firestore returning metadata after call from expo environment 【发布时间】:2021-07-31 01:59:36 【问题描述】:

大家好,我是反应原生的新手。我正在使用 expo env 的 firebase 包将 firebase 连接到我的项目。 auth 和添加 db 工作正常,但是当我尝试从 firestore 获取数据时,我得到了很多元数据并且无法解析它。

这是我的调用函数。

const getUserData = async () => 
        const uid = await AuthUser.getUser();
        try        
            const data = await firebase.firestore().collection('users').get();
            data.docs.forEach(doc => 
                console.log(doc.data())
            );
         catch (error) 
            console.log("error while getting user details", error)
        
    ; 

这是我得到的结果

Array [
  Zu 
    "_": vu 
      "Hf": Ku 
        "firestore": Qu 
          "INTERNAL": Object 
            "delete": [Function _delete],
          ,
          "Id": qu ,
          "_": tu 
            "A_": FirebaseAppImpl 
              "automaticDataCollectionEnabled_": false,
              "container": ComponentContainer 
                "name": "[DEFAULT]",
                "providers": Map 
                  "app" => Provider 
                    "component": Component 
                      "instanceFactory": [Function anonymous],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "app",
                      "serviceProps": Object ,
                      "type": "PUBLIC",
                    ,
                    "container": [Circular],
                    "instances": Map 
                      "[DEFAULT]" => [Circular],
                    ,
                    "instancesDeferred": Map ,
                    "name": "app",
                  ,
                  "platform-logger" => Provider 
                    "component": Component 
                      "instanceFactory": [Function anonymous],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "platform-logger",
                      "serviceProps": Object ,
                      "type": "PRIVATE",
                    ,
                    "container": [Circular],
                    "instances": Map ,
                    "instancesDeferred": Map ,
                    "name": "platform-logger",
                  ,
                  "fire-core-rn-version" => Provider 
                    "component": Component 
                      "instanceFactory": [Function anonymous],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "fire-core-rn-version",
                      "serviceProps": Object ,
                      "type": "VERSION",
                    ,
                    "container": [Circular],
                    "instances": Map ,
                    "instancesDeferred": Map ,
                    "name": "fire-core-rn-version",
                  ,
                  "fire-js-version" => Provider 
                    "component": Component 
                      "instanceFactory": [Function anonymous],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "fire-js-version",
                      "serviceProps": Object ,
                      "type": "VERSION",
                    ,
                    "container": [Circular],
                    "instances": Map ,
                    "instancesDeferred": Map ,
                    "name": "fire-js-version",
                  ,
                  "auth" => Provider 
                    "component": Object 
                      "instanceFactory": [Function instanceFactory],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "auth",
                      "serviceProps": Object 
                        "ActionCodeInfo": Object 
                          "Operation": Object 
                            "EMAIL_SIGNIN": "EMAIL_SIGNIN",
                            "PASSWORD_RESET": "PASSWORD_RESET",
                            "RECOVER_EMAIL": "RECOVER_EMAIL",
                            "REVERT_SECOND_FACTOR_ADDITION": "REVERT_SECOND_FACTOR_ADDITION",
                            "VERIFY_AND_CHANGE_EMAIL": "VERIFY_AND_CHANGE_EMAIL",
                            "VERIFY_EMAIL": "VERIFY_EMAIL",
                          ,
                        ,
                        "ActionCodeURL": [Function d],
                        "Auth": [Function En],
                        "AuthCredential": [Function Bg],
                        "EmailAuthProvider": [Function d],
                        "Error": [Function t],
                        "FacebookAuthProvider": [Function d],
                        "GithubAuthProvider": [Function d],
                        "GoogleAuthProvider": [Function d],
                        "OAuthProvider": [Function d],
                        "PhoneAuthProvider": [Function d],
                        "PhoneMultiFactorGenerator": [Function d],
                        "RecaptchaVerifier": [Function d],
                        "SAMLAuthProvider": [Function d],
                        "TwitterAuthProvider": [Function d],
                      ,
                      "type": "PUBLIC",
                    ,
                    "container": [Circular],
                    "instances": Map 
                      "[DEFAULT]" => Object 
                        "apiKey": "xxxx",
                        "appName": "[DEFAULT]",
                        "authDomain": "xxxx",
                        "currentUser": Object 
                          "apiKey": "xxxx",
                          "appName": "[DEFAULT]",
                          "authDomain": "xxxx",
                          "createdAt": "xxxx",
                          "displayName": null,
                          "email": "sayanc20002@gmail.com",
                          "emailVerified": false,
                          "isAnonymous": false,
                          "lastLoginAt": "1620559912736",
                          "multiFactor": Object 
                            "enrolledFactors": Array [],
                          ,
                          "phoneNumber": null,
                          "photoURL": null,
                          "providerData": Array [
                            Object 
                              "displayName": null,
                              "email": "sayanc20002@gmail.com",
                              "phoneNumber": null,
                              "photoURL": null,
                              "providerId": "password",
                              "uid": "sayanc20002@gmail.com",
                            ,
                          ],
                          "redirectEventId": null,
                          "stsTokenManager": Object 
                            "accessToken": "xxxx",
                            "apiKey": "xxxx",
                            "expirationTime": 1620563512726,
                            "refreshToken": "xxxx",
                          ,
                          "tenantId": null,
                          "uid": "uoGEBWjWHMfXhBL4dVodHAiHr7d2",
                        ,
                      ,
                    ,
                    "instancesDeferred": Map ,
                    "name": "auth",
                  ,
                  "auth-internal" => Provider 
                    "component": Object 
                      "instanceFactory": [Function instanceFactory],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "auth-internal",
                      "type": "PRIVATE",
                    ,
                    "container": [Circular],
                    "instances": Map 
                      "[DEFAULT]" => Object 
                        "addAuthTokenListener": [Function bound ],
                        "getToken": [Function bound ],
                        "getUid": [Function bound ],
                        "removeAuthTokenListener": [Function bound ],
                      ,
                    ,
                    "instancesDeferred": Map ,
                    "name": "auth-internal",
                  ,
                  "fire-auth-version" => Provider 
                    "component": Component 
                      "instanceFactory": [Function anonymous],
                      "instantiationMode": "LAZY",
                      "multipleInstances": false,
                      "name": "fire-auth-version",
                      "serviceProps": Object ,
                      "type": "VERSION",
                    ,
                    "container": [Circular],
                    "instances": Map ,
                    "instancesDeferred": Map ,
                    "name": "fire-auth-version",
                  ,
                  "database" => Provider 
                    "component": Component 
                      "instanceFactory": [Function anonymous],
                      "instantiationMode": "LAZY",
                      "multipleInstances": true,
                    ...(truncated to the first 10000 characters)

数据库使用公共字段 uid 链接到 auth。 另外,如果你们能帮助我如何根据 uid 搜索集合中的文档

【问题讨论】:

【参考方案1】:

该日志输出看起来像是来自 console.log(doc.data()) 以外的其他地方,因为它包含不应由 data() 返回的内部数据结构。

但无论哪种方式,要从文档中获取字段值,请使用 Firebase javascript SDK 和/或 ReactNative Firebase 的文档。

例如,如果您在用户文档中有 name 字段,则应如下所示:

console.log(doc.data()['name'])

【讨论】:

非常感谢我解决了这个问题。

以上是关于从 expo 环境调用后 Firestore 返回元数据的主要内容,如果未能解决你的问题,请参考以下文章

Expo + firebase@9.0.1/9.0.0: @firebase/firestore:, Firestore (9.0.0): 无法到达 Cloud Firestore 后端

从 Expo 调用本地托管的服务器

SwiftUI - 等到 Firestore getDocuments() 完成后再继续

firestore 不适用于 React Native Expo

使用signInWithCredential(),React Native Expo时,Firestore权限被拒绝

Firebase Firestore onSnapshot PayloadTooLargeError:Expo / React Native 项目上的请求实体太大