0

Here is the function I am using for accessing the firebase data.

export class SubscriptionProductComponent implements OnInit {

  private products: any;
  private customer: any;

  constructor(private database: AngularFireDatabase,private route: Router) { 
      this.customer = this.database.database.ref().child('/Products/');

    this.customer.orderByChild("productType").equalTo("subscription").on('value', function (snapshot) {
      console.log(snapshot.toJSON());
      this.products = snapshot.toJSON();
      console.log(this.products);
    });
 }

  ngOnInit() {}

  //Some other function are here
}

I am getting the error that

core.js:6210 ERROR TypeError: Cannot set property 'products' of undefined
    at subscription-product.component.ts:34
    at valueCallback (index.esm.js:14463)
    at CallbackContext.push.6Uf2.CallbackContext.onValue (index.esm.js:12274)
    at index.esm.js:13118
    at exceptionGuard (index.esm.js:638)
    at eventListRaise (index.esm.js:11082)
    at eventQueueRaiseQueuedEventsMatchingPredicate (index.esm.js:11057)
    at eventQueueRaiseEventsForChangedPath (index.esm.js:11044)
    at repoOnDataUpdate (index.esm.js:11261)
    at PersistentConnection.repo.server_ [as onDataUpdate_] (index.esm.js:11163)

Please help me with this one.

Kunal Chikte
  • 29
  • 1
  • 8

1 Answers1

1

Do it with an arrow function:

this.customer.orderByChild("productType").equalTo("subscription").on('value', (snapshot) => {
  console.log(snapshot.toJSON());
  this.products = snapshot.toJSON();
  console.log(this.products);
});