Sivuyile TG Magutywa
Sivuyile TG Magutywa

Reputation: 1271

TypeError: undefined is not an object (evaluating 'ref.onSnapshot')

Angular and Firebase Firestore throws TypeError: undefined is not an object (evaluating 'ref.onSnapshot')

this.fbstore
      .collection(`claims`, ref => {
        let query:
          | firebase.default.firestore.CollectionReference
          | firebase.default.firestore.Query = ref;

          if(someValue === true) {
            return query.where('field1', '==', 20);
          }

          
      }).valueChanges();

Upvotes: 0

Views: 83

Answers (1)

Sivuyile TG Magutywa
Sivuyile TG Magutywa

Reputation: 1271

The problem is if someValue is false then query is not returned, adding else statement will solve the issue or add default return query. These types of errors are time consuming if you rely on IDE, Typescript to much, it's easy to see the problem on this sample code.

this.fbstore
      .collection(`claims`, ref => {
        let query:
          | firebase.default.firestore.CollectionReference
          | firebase.default.firestore.Query = ref;

          if(someValue === true) {
            return query.where('field1', '==', 20);
          }
       
         return query; // this will not run if someValue is true

      }).valueChanges();

Upvotes: 1

Related Questions