How can I run an observable with data obtained from firebase

0

I need to go through an observable obtained from firebase, where I compare a id , to be able to create a array with the data belonging to this id .

 '  import { Component } from '@angular/core';

   import { NavController, AlertController } from 'ionic-angular';
 //import { DaggService } from '../../providers/dagg-service';
  import { FirebaseListObservable, AngularFireDatabase  } from 'angularfire2';
  import 'rxjs/add/operator/map';
  import 'rxjs/Rx';


@Component({
   selector: 'page-home',
   templateUrl: 'home.html'/*,
  providers:[ DaggService ]*/
})
 export class HomePage {

 regiones: FirebaseListObservable<any>;
 provincias: FirebaseListObservable<any>;
 ciudades: FirebaseListObservable<any>;

/*public regiones: any;
public provincias: any;
public ciudades: any;*/


 constructor(
        public navCtrl: NavController,
        ///public ds: DaggService//,
        public alertCtrl: AlertController,
        public database: AngularFireDatabase
        ) {
            /*this.regiones = [];
            this.provincias = [];
            this.ciudades = [];*/

            // AQUÍ EXTRAIGO LOS DATOS 
            this.regiones = this.database.list('/regiones');
            this.provincias = this.database.list('/provincias');
            this.ciudades = this.database.list('/ciudades');
            console.log(this.regiones)
        }
 getCiudades(provincia){

    console.log(provincia)

}
ionViewDidLoad(){


} 



}
 '

According to the id obtained, ion-select goes through the observable of the cities, which contains: IDCIUDAD , NAME CITY e < strong> IDPROVINCIA , to then compare the ** IDPROVINCIA * returned by the ion-select with that of the observable and create an array of cities depending on the ID of the provinces.

    
asked by Dagg 08.04.2017 в 03:35
source

1 answer

1

I wanted to write a comment instead of an answer, but the system asks me to have at least 50 points to do it.

In order to obtain the data of an Observable you need to subscribe to them. Therefore, I would do the following:

'

export class HomePage {

public regiones: any = [];
public provincias: any = [];
public ciudades: any = [];


 constructor(
        public navCtrl: NavController,
        ///public ds: DaggService//,
        public alertCtrl: AlertController,
        public database: AngularFireDatabase
        ) {
            // AQUÍ EXTRAIGO LOS DATOS 
            this.database.list('/regiones').subscribe( 
                regiones => this.regiones = regiones,
                error => console.log(error)
            );
            this.database.list('/provincias').subscribe( 
                provincias => this.provincias = provincias,
                error => console.log(error)
            );
            this.database.list('/ciudades').subscribe( 
                ciudades => this.ciudades = ciudades,
                error => console.log(error)
            );
            console.log(this.regiones)
        }
 getCiudades(provincia){

    this.ciudades.foreach( ciudad =>{
         //Aquí insertas el código de comparación que necesites
    });

}
ionViewDidLoad(){


} 

' I hope it serves you.

    
answered by 11.04.2017 / 05:36
source