简体   繁体   English

Firebase Firestore 集合获取方法导致错误

[英]Firebase Firestore collection get method causing error

I have a collection called 'users' in Firestore, and it is structured like this我在 Firestore 中有一个名为“用户”的集合,它的结构如下

users -> document -> email, name用户 -> 文档 -> email,名称

db.collection('users').get().then(snapshot => {
  snapshot.docs.forEach(doc => {
    console.log(doc)
  });
});

This code is supposed to fetch all the documents in the collection (there are about 15), but when I run it, I get a long error (below)这段代码应该获取集合中的所有文档(大约有 15 个),但是当我运行它时,出现了一个很长的错误(下图)

QueryDocumentSnapshot {
  _firestore: Firestore {
    _delegate: Firestore$1 {
      type: 'firestore',
      _persistenceKey: '[DEFAULT]',
      _settings: [FirestoreSettingsImpl],
      _settingsFrozen: true,
      _app: [FirebaseAppImpl],
      _databaseId: [DatabaseId],
      _credentials: [FirebaseCredentialsProvider],
      _queue: [AsyncQueueImpl],
      _firestoreClient: [FirestoreClient]
    },
    _persistenceProvider: IndexedDbPersistenceProvider {},
    INTERNAL: { delete: [Function: delete] },
    _appCompat: FirebaseAppImpl {
      firebase_: [Object],
      isDeleted_: false,
      name_: '[DEFAULT]',
      automaticDataCollectionEnabled_: false,
      options_: [Object],
      container: [ComponentContainer]
    }
  },
  _delegate: QueryDocumentSnapshot$1 {
    _firestore: Firestore$1 {
      type: 'firestore',
      _persistenceKey: '[DEFAULT]',
      _settings: [FirestoreSettingsImpl],
      _settingsFrozen: true,
      _app: [FirebaseAppImpl],
      _databaseId: [DatabaseId],
      _credentials: [FirebaseCredentialsProvider],
      _queue: [AsyncQueueImpl],
      _firestoreClient: [FirestoreClient]
    },

How should I fetch documents properly without this error?我应该如何在没有此错误的情况下正确获取文档?

Firebase version - 8.2.3 Firebase 版本 - 8.2.3

Thanks谢谢

This is not an error: With your code you are actually printing the QueryDocumentSnapshot Objects in your console.这不是错误:使用您的代码,您实际上是在控制台中打印QueryDocumentSnapshot对象。

You should call the data() method to get the documents data, as follows:您应该调用data()方法来获取文档数据,如下所示:

db.collection('users').get().then(snapshot => {
  snapshot.docs.forEach(doc => {
    console.log(doc.data());  // Or console.log(JSON.stringify(doc.data())); 
  });
});

Also note that a QuerySnapshot has a forEach() method and therefore you could do:还要注意QuerySnapshot有一个forEach()方法,因此你可以这样做:

db.collection('users').get().then(snapshot => {
  snapshot.forEach(doc => {
    console.log(doc.data());
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM