Search code examples
javascriptangularfirebasegoogle-cloud-firestoreangularfire2

Gets data from the Firestore Database along with the query


I only need to download data from the Firestore Database that is equal to something. It crashes me an error:

Cannot read property 'onSnapshot' of undefined

How can I get data from Firestore and create a query?

My code:

const messages = this.firestore.collection('chat', ref => {
    ref.where('receiver_id', '==', employee.user.id);
    }).valueChanges().subscribe((message) => {
        console.log(message);
    });

My Firestore Database:

My Firestore Database


Solution

  • You're not returning anything from within the callback, which means that AngularFire ends up trying to call onSnapshot on undefined.

    It should be either:

    const messages = this.firestore.collection('chat', ref => {
        return ref.where('receiver_id', '==', employee.user.id);
    }).valueChanges().subscribe((message) => {
        console.log(message);
    });
    

    Or more commonly written as:

    const messages = this.firestore.collection('chat', ref => ref.where('receiver_id', '==', employee.user.id))
    .valueChanges().subscribe((message) => {
        console.log(message);
    });