FattyDev1
FattyDev1

Reputation: 106

Firebase Firestore collection get method causing error

I have a collection called 'users' in Firestore, and it is structured like this

users -> document -> email, name

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)

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

Thanks

Upvotes: 0

Views: 413

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

This is not an error: With your code you are actually printing the QueryDocumentSnapshot Objects in your console.

You should call the data() method to get the documents data, as follows:

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:

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

Upvotes: 2

Related Questions