使用 Firestore 的控制台日志输出的意外行为

Unexpected behaviour from console log output with Firestore

我决定从实时数据库过渡到 Firestore,但是我 运行 遇到了一些意外问题。很简单:浏览器控制台输出了非常意外的结果,但是 exist 之类的方法仍然可以正常工作。

const firebase = require('firebase/app');
require('firebase/firestore');

const firebaseConfig = {
   the usual
};

firebase.initializeApp(firebaseConfig);

const firestore = firebase.firestore();

(async () => {
    const userRef = firestore.doc('users/fjafSAe2VQRWx7g9ocxn')
    const snapShot = await userRef.get();
    console.log(snapShot)
    console.log(snapShot.exists)
})()

现在 console.log 在浏览器中出现一些我无法理解的乱码:

t {pf: e, w_: t, Nf: n, Ff: false, $f: false, …}
$f: false
Ff: false
Nf: n {key: t, version: t, nn: t, Ye: false, hasCommittedMutations: false}
m_: null
pf: e {cf: n, uf: e, hf: FirebaseAppImpl, lf: e, INTERNAL: {…}, …}
w_: t {path: n}
exists: (...)
id: (...)
metadata: (...)
ref: (...)
__proto__: Object

但是 snapShot.exists 按预期工作。更奇怪的是,当我在 Node.js 中控制台记录以上内容时,它会输出 DocumentSnapshot

DocumentSnapshot {
  _firestore:
   Firestore {
     _offlineComponentProvider:
      MultiTabOfflineComponentProvider { onlineComponentProvider: OnlineComponentProvider {} },
     _onlineComponentProvider: OnlineComponentProvider {},
     _firebaseApp:
      FirebaseAppImpl {
        firebase_: [Object],
        isDeleted_: false,
        name_: '[DEFAULT]',
        automaticDataCollectionEnabled_: false,
        options_: [Object],
        container: [ComponentContainer] },
     _queue:
      AsyncQueue {
        tail: [Promise],
        retryableOps: [],
        _isShuttingDown: false,
        delayedOperations: [Array],
        failure: null,
        operationInProgress: true,
        timerIdsToSkip: [],
        backoff: [ExponentialBackoff],
        visibilityHandler: [Function] },
     INTERNAL: { delete: [Function: delete] },
     _databaseId:
      DatabaseId { projectId: 'ecommerce-33931', database: '(default)' },
     _persistenceKey: '[DEFAULT]',
     _credentials:
      FirebaseCredentialsProvider {
        tokenListener: [Function],
        currentUser: [User],
        receivedInitialUser: true,
        tokenCounter: 1,
        changeListener: [Function],
        forceRefresh: false,
        auth: null },
     _settings:
      FirestoreSettings {
        host: 'firestore.googleapis.com',
        ssl: true,
        credentials: undefined,
        timestampsInSnapshots: true,
        ignoreUndefinedProperties: false,
        cacheSizeBytes: 41943040,
        experimentalForceLongPolling: false },
     _firestoreClient:
      FirestoreClient {
        credentials: [FirebaseCredentialsProvider],
        asyncQueue: [AsyncQueue],
        clientId: 'exM4ONFeK4q2tpdTjiyl',
        initializationDone: [Deferred],
        databaseInfo: [DatabaseInfo],
        persistence: [MemoryPersistence],
        sharedClientState: [MemorySharedClientState],
        localStore: [LocalStoreImpl],
        gcScheduler: null,
        datastore: [DatastoreImpl],
        remoteStore: [RemoteStoreImpl],
        syncEngine: [SyncEngineImpl],
        eventMgr: [EventManagerImpl] } },
  _key:
   DocumentKey { path: ResourcePath { segments: [Array], offset: 0, len: 2 } },
  _document:
   Document {
     key: DocumentKey { path: [ResourcePath] },
     version: SnapshotVersion { timestamp: [Timestamp] },
     objectValue: ObjectValue { proto: [Object] },
     hasLocalMutations: false,
     hasCommittedMutations: false },
  _fromCache: false,
  _hasPendingWrites: false,
  _converter: null }

然而,存在 属性 未在此处列出(但仍记录为 true)。知道是什么原因造成的吗?我在 React 工作,当它不起作用时也在 Node 中尝试过。

您看到的是 DocumentSnapshot type object. It doesn't really make a whole lot of sense to examine its contents this way, because it's an internally complex object that itself contains many references to other objects that are also being stringified here. If you want to see the document data inside that snapshot, you should simply call data() 的默认字符串化表示形式,以获得一个简单的 JavaScript 对象,可以轻松记录。

console.log(snapShot.data())