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

问题描述

我决定进行从实时数据库到Firestore的过渡,但是我遇到了一些意外问题。这很简单:浏览器控制台会注销出乎意料的结果,但是像现存的方法仍然可以按预期工作。

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 }

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

解决方法

您看到的是DocumentSnapshot类型对象的默认字符串表示形式。以这种方式检查其内容并没有多大意义,因为它是一个内部复杂的对象,它本身包含许多对其他对象的引用,这些对象也在此进行了字符串化。如果要查看快照中的文档数据,只需在其上调用data()即可获得一个易于记录的简单JavaScript对象。

"%n"