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

Asked

Viewed 1,226 times

0

Gist Referring to the question

I have an Event class, and a method q is called snapdbEvents(), but when I put the object inside and try to define the values of the variables in the object occurs the following error:

inserir a descrição da imagem aqui

Print by DB

inserir a descrição da imagem aqui

  • you have already checked if the id property has any value? by error it is Undefined or without a defined type

  • Yes, I can print out all the data on the console.

  • instead of using snapshot.key in the id tries to place a string manually only for testing then

  • I tried and gave the same thing,

  • 1

    because the in id id?:string has the question and in others not

1 answer

0


When assigning the values in the class it is recommended to use setters 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 Komponent or Provider:

private evento:Evento

chamaAPI() {
  apiService.getEventos().subscribe(res => {
    this.evento = new Evento(res.evento);
  });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.