How to create an object and populate with data returned from Firebse?

0

Gist Regarding Question

I have an event class, and a method that is called snapdbEvent (), but when I put the object in it and try to define the values of the variables in the object, the following error occurs:

PrintfromDB

    
asked by anonymous 03.08.2017 / 13:38

1 answer

0

When assigning the values in the class it is recommended to use setters functions or a constructor, for example:

class export Evento {
  id: string;
  horaInicio: string;
  horaFim: string;
  
  constructor(values: Object) {
    Object.assign(this, values);
    // Ou
    this.id = values.id;
    this.horaInicio = values.horaInicio;
    this.horaFim = values.horaFim;
  }
  
  setHoraInicio(horaInicio) {
    this.horaInicio = horaInicio;
  }
  
  setHoraFim(horaFim) {
    this.horaFim = horaFim;
  }

}

and its attribution method in the component or provider:

private evento:Evento

chamaAPI() {
  apiService.getEventos().subscribe(res => {
    this.evento = new Evento(res.evento);
  });
}
    
19.07.2018 / 20:08