0

I have a "service" in setting part of my app. I push data in Firebase. In another component i have form with "md-select" that read the same data. The problem is that I can not initialize the list. Coming out and going back into the component everything works correctly. The list populates as expected.

this is my service in setting part.

import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Carburante } from './carburante.model';

@Injectable()
export class CarburantiService {

  private basePath: string = '/carburanti';

  carburanti: FirebaseListObservable<Carburante[]> = null; //  list of objects  
  carburante: FirebaseObjectObservable<Carburante> = null; //   single object
  userId: string;//_____

  constructor(private db: AngularFireDatabase,
    private afAuth: AngularFireAuth) {
    this.afAuth.authState.subscribe(user => {
      if (user) this.userId = user.uid
    })
    this.carburanti = db.list(`/carburanti`);    
  }

    getCarburantiList(query = {}): FirebaseListObservable<Carburante[]> {
    if (!this.userId) return;
    this.carburanti = this.db.list(`carburanti/${this.userId}`, { query: query }
    );
    return this.carburanti
  }
...some code....

and the component:

import { Component, OnInit} from '@angular/core';
import { ReactiveFormsModule, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Carico } from '../carico.model';
import { CarichiService } from '../carichi.service';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
...some code...
import { Carburante } from '../../impostazioni/carburanti/carburante.model';
import { CarburantiService } from '../../impostazioni/carburanti/carburanti.service';

@Component({
  selector: 'app-new-carico-form',
  templateUrl: './new-carico-form.component.html',
  styleUrls: ['./new-carico-form.component.css'],


})
export class NewCaricoFormComponent implements OnInit{      
  carburanti: FirebaseListObservable<Carburante[]>;
  carico: Carico = new Carico();
  caricoForm: FormGroup;      

  constructor(private fb: FormBuilder,
    private carichiService: CarichiService,
    private db: AngularFireDatabase,        
    private carburantiService: CarburantiService,   
  ) {          
    this.createForm();
  }

  ngOnInit() {  
this.carburanti = this.carburantiService.getCarburantiList() ;         
  }    
  createForm() {
    this.caricoForm = this.fb.group({
      dataCarico: ['', Validators.required],
      riferimentoCarico: [''],
      dettaglio: this.fb.group({        
        carburante: ['' ,Validators.required],
        quantita: ['',Validators.required]
      })
    });
  }

  ...some code...

why if I call the list in my ngOnit part, doesn't work? thanks

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • 1
    What is the problem exactly. I'm not completely understanding where the issue is? What is not working as you state in the beginning that everything works as should? :) What exactly is not working, and what does **not working** specifically mean? – AT82 Aug 09 '17 at 20:10
  • I'm working on it, I think is problem with auth. I have an app with "setting" part where I set stuff (e.g. set fuel.name) and in a dashboard form I have md-select that pull that stuff (e.g. [name1, name2, name3]). I the first load the md-select is undefined because i don't have userId, if I close and open again it works. thanks, i will work on it. – Antonino Mirabile Aug 09 '17 at 22:17

0 Answers0