Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularFIRE Property 'subscribe' does not exist on type 'AngularFireList<{}>'

I'm following this tutorial about how to connect angular with firebase database. But in minute 17:30 I'm getting this error:

Property 'subscribe' does not exist on type 'AngularFireList<{}>'

my AppComponent:

import { Component } from '@angular/core';
import {AngularFireDatabase, AngularFireDatabaseModule} from 'angularfire2/database';

import {AngularFireAuth, AngularFireAuthModule} from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';
import { Country } from './models/country';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  countries: any[];

  constructor(db: AngularFireDatabase )
  {
     db.list('/Country/countries')
     .subscribe(countries => { //  <--ERROR IS HERE
      this.countries = countries;
      console.log(this.countries);
      });
  }
}

My model:

export class Country {
     // --ATTRIB--
     id: string;
     name: string;
     code: string;
     urlFlag: string;
}

Cant find anything about this error. I'm beginner in angular. Thanks if can help me.

like image 971
Diego Venâncio Avatar asked Oct 13 '17 16:10

Diego Venâncio


4 Answers

Starting in AngularFire 5.0 you'll want to use one of snapshotChanges(), valueChanges<T>(), stateChanges(), or auditTrail(). See the 5.0 migration guide.

Get started with the most basic, valueChanges():

export class AppComponent {
  countries: Observable<Country[]>;
  constructor(db: AngularFireDatabase ) {
    this.countries = db.list('/Country/countries').valueChanges();
  }
}
like image 198
James Daniels Avatar answered Oct 21 '22 02:10

James Daniels


A simpler change would to be add valueChanges() before .subscribe()

db.list('/Country/countries').valueChanges().subscribe(countries => {
    this.countries = countries;
    console.log(this.countries);
});
like image 26
alex Avatar answered Oct 21 '22 00:10

alex


export class AppComponent {
  constructor(db : AngularFireDatabase){
 db.list('/courses').valueChanges().subscribe()
  }
}

In Angular firebase version 5.0 and above .subscribe is available after .valuechanges()

like image 2
Simran kaur Avatar answered Oct 21 '22 02:10

Simran kaur


import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs';

Import code the code,

 countries: Observable<any[]>;
 allCountries: any;

  constructor(db: AngularFireDatabase ) {
    this.countries = db.list('/yourpath').valueChanges();  
    this.countries.subscribe(countries => {
      this.allCountries = countries;
      console.log(this.allCountries);
    })
    console.log(this.countries)
  }
like image 1
Mehmet Karakose Avatar answered Oct 21 '22 02:10

Mehmet Karakose