React-Native-Firebase 传播 auth().currentUser 对象 {...auth().currentUser}

React-Native-Firebase Spread auth().currentUser Object {...auth().currentUser}

当我 Console.Log(auth().currentUser) 我得到以下结果:

Object {
  "displayName": "Test",
  "email": "test@gmail.com",
  "emailVerified": false,
  "isAnonymous": false,
  "metadata": Object {
    "creationTime": 1638471731312,
    "lastSignInTime": 1648765363821,
  },
  "phoneNumber": null,
  "photoURL": "www.google.com",
  "providerData": Array [
    Object {
      "displayName": "Test",
      "email": "test@gmail.com",
      "photoURL": "www.google.com",
      "providerId": "password",
      "uid": "test@gmail.com",
    },
  ],
  "providerId": "firebase",
  "refreshToken": "...",
  "tenantId": null,
  "uid": "...",
}

当我 Console.Log({...auth().currentUser}) 我期待相同的结果。 相反,我得到以下信息:

Object {
"_auth": FirebaseAuthModule {
  "_app": FirebaseApp {
    "_automaticDataCollectionEnabled": true,
    "_deleteApp": [Function bound deleteApp],
    "_deleted": false,
    "_initialized": true,
    "_name": "[DEFAULT]",
    "_nativeInitialized": true,
    "_options": Object {
      "apiKey": "...",
      "appId": "...",
      "clientId": "...",
      "databaseURL": "...",
      "messagingSenderId": "...",
      "projectId": "...",
      "storageBucket": "...",
    },
  },
  "_authResult": true,
  "_config": Object {
    "ModuleClass": [Function FirebaseAuthModule],
    "hasCustomUrlOrRegionSupport": false,
    "hasMultiAppSupport": true,
    "namespace": "auth",
    "nativeEvents": Array [
      "auth_state_changed",
      "auth_id_token_changed",
      "phone_auth_state_changed",
    ],
    "nativeModuleName": "RNFBAuthModule",
    "statics": Object {
      "AppleAuthProvider": [Function AppleAuthProvider],
      "EmailAuthProvider": [Function EmailAuthProvider],
      "FacebookAuthProvider": [Function FacebookAuthProvider],
      "GithubAuthProvider": [Function GithubAuthProvider],
      "GoogleAuthProvider": [Function GoogleAuthProvider],
      "OAuthProvider": [Function OAuthProvider],
      "PhoneAuthProvider": [Function PhoneAuthProvider],
      "PhoneAuthState": Object {
        "AUTO_VERIFIED": "verified",
        "AUTO_VERIFY_TIMEOUT": "timeout",
        "CODE_SENT": "sent",
        "ERROR": "error",
      },
      "TwitterAuthProvider": [Function TwitterAuthProvider],
    },
    "version": "14.7.0",
  },
  "_customUrlOrRegion": undefined,
  "_languageCode": undefined,
  "_nativeModule": Object {
    "APP_LANGUAGE": Object {},
    "APP_USER": Object {
      "[DEFAULT]": Object {
        "displayName": "Test",
        "email": "test@gmail.com",
        "emailVerified": false,
        "isAnonymous": false,
        "metadata": Object {
          "creationTime": 1638471731312,
          "lastSignInTime": 1648765363821,
        },
        "phoneNumber": null,
        "photoURL": "www.google.com",
        "providerData": Array [
          Object {
            "displayName": "Test",
            "email": "test@gmail.com",
            "photoURL": "www.google.com",
            "providerId": "password",
            "uid": "test@gmail.com",
          },
        ],
        "providerId": "firebase",
        "refreshToken": "...",
        "tenantId": null,
        "uid": "...",
      },
    },
    "addAuthStateListener": [Function anonymous],
    "addIdTokenListener": [Function anonymous],
    "applyActionCode": [Function anonymous],
    "checkActionCode": [Function anonymous],
    "confirmPasswordReset": [Function anonymous],
    "confirmationResultConfirm": [Function anonymous],
    "createUserWithEmailAndPassword": [Function anonymous],
    "delete": [Function anonymous],
    "fetchSignInMethodsForEmail": [Function anonymous],
    "getConstants": [Function anonymous],
    "getIdToken": [Function anonymous],
    "getIdTokenResult": [Function anonymous],
    "linkWithCredential": [Function anonymous],
    "reauthenticateWithCredential": [Function anonymous],
    "reload": [Function anonymous],
    "removeAuthStateListener": [Function anonymous],
    "removeIdTokenListener": [Function anonymous],
    "sendEmailVerification": [Function anonymous],
    "sendPasswordResetEmail": [Function anonymous],
    "sendSignInLinkToEmail": [Function anonymous],
    "setAppVerificationDisabledForTesting": [Function anonymous],
    "setLanguageCode": [Function anonymous],
    "setTenantId": [Function anonymous],
    "signInAnonymously": [Function anonymous],
    "signInWithCredential": [Function anonymous],
    "signInWithCustomToken": [Function anonymous],
    "signInWithEmailAndPassword": [Function anonymous],
    "signInWithEmailLink": [Function anonymous],
    "signInWithPhoneNumber": [Function anonymous],
    "signOut": [Function anonymous],
    "unlink": [Function anonymous],
    "updateEmail": [Function anonymous],
    "updatePassword": [Function anonymous],
    "updatePhoneNumber": [Function anonymous],
    "updateProfile": [Function anonymous],
    "useDeviceLanguage": [Function anonymous],
    "useEmulator": [Function anonymous],
    "useUserAccessGroup": [Function anonymous],
    "verifyBeforeUpdateEmail": [Function anonymous],
    "verifyPasswordResetCode": [Function anonymous],
    "verifyPhoneNumber": [Function anonymous],
  },
  "_settings": null,
  "_tenantId": null,
  "_user": Object {
    "displayName": "Test",
    "email": "test@gmail.com",
    "emailVerified": false,
    "isAnonymous": false,
    "metadata": Object {
      "creationTime": 1638471731312,
      "lastSignInTime": 1648765363821,
    },
    "phoneNumber": null,
    "photoURL": "www.google.com",
    "providerData": Array [
      Object {
        "displayName": "Test",
        "email": "test@gmail.com",
        "photoURL": "www.google.com",
        "providerId": "password",
        "uid": "test@gmail.com",
      },
    ],
    "providerId": "firebase",
    "refreshToken": "...",
    "tenantId": null,
    "uid": "...",
  },
},
"_user": Object {
  "displayName": "Test",
  "email": "test@gmail.com",
  "emailVerified": false,
  "isAnonymous": false,
  "metadata": Object {
    "creationTime": 1638471731312,
    "lastSignInTime": 1648765363821,
  },
  "phoneNumber": null,
  "photoURL": "www.google.com",
  "providerData": Array [
    Object {
      "displayName": "Test",
      "email": "test@gmail.com",
      "photoURL": "www.google.com",
      "providerId": "password",
      "uid": "test@gmail.com",
    },
  ],
  "providerId": "firebase",
  "refreshToken": "...",
  "tenantId": null,
  "uid": "...",
},

这是预期的行为吗?为什么会这样? 传播运算符应该克隆传递给它的对象,不是吗?

像这样:

let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };

let clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }
let mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

auth().currentUser实际上是一个User account对象,里面有很多属性和方法

来自 MDN

The spread operator copies own enumerable properties from a provided object onto a new object.

您可以使用 .toJSON() 获取此用户帐户对象的 JSON-serializable 表示。

console.log({ ...auth().currentUser.toJSON() })