Search code examples
javascriptfirebasegoogle-cloud-firestoreangularfire2

firebase .then() is not a function


I'm attempting to get all documents in a collection.

import { Injectable } from '@angular/core';
import {AngularFirestore} from '@angular/fire/firestore';
import {Router} from '@angular/router';


@Injectable({
  providedIn: 'root'
})
export class FirebaseService {

  constructor(public db: AngularFirestore, public router: Router) { }
    getAll() {
     this.db.collection('Completed-Forms').get().then((querySnapshot) => {
       querySnapshot.forEach((doc) => {
         // doc.data() is never undefined for query doc snapshots
         console.log(doc.id, ' => ', doc.data());
      });
    });
  }
}

My error:

Uncaught (in promise): TypeError: this.db.collection(...).get(...).then is not a function

TypeError: this.db.collection(...).get(...).then is not a function

Does anyone know what is going wrong?


Solution

  • You're using AngularFire2, which exposes the documents as an observable.

    If you want to use the regular JavaScript SDK, do

    firebase.firestore().collection('Completed-Forms').get().then((querySnapshot) => {
    

    Or (untested, but should work according to the TypeScript declarations):

    this.db.firestore.collection('Completed-Forms').get().then((querySnapshot) => {