2017-11-02 1 views
0

Ich habe diese Struktur in meinem Feuerbasis:einfügen Feuerbasis Werte in eine Klasse enthält ein Array

[ 
    { 
     "numero":1 
    }, 
    { 
     "cpfProprietario":11122233344, 
     "moradores":{ 
     "1112121212":{ 
      "cpf":"323232", 
      "nome":"teste", 
      "uid":"dfdsifnds", 
      "urlFoto":"/assets/ic_account.svg" 
     }, 
     "11122233344":{ 
      "cpf":"11122233344", 
      "nome":"Maria", 
      "uid":"X59CZYkqDAYRIRzHt6leWuCDOzD3", 
      "urlFoto":"/assets/ic_account.svg" 
     } 
     }, 
     "numero":2 
    }, 
    { 
     "numero":3 
    } 
] 

das einzige Attribut, das ich immer enthalten ist die numero.

Ich brauche diese Werte in ein Array dieser Klasse zu speichern:

export class Lote { 
    public numero: number; 
    public cpfProprietario: string; 
    public moradores: MyMorador[] = []; 

    addMorador(cpf: string, uid: string, nome: string, urlFoto: string) { 
    this.moradores.push(new MyMorador(cpf, uid, nome, urlFoto)); 
    } 
} 

class MyMorador { 
    cpf: string; 
    uid: string; 
    nome: string; 
    urlFoto: string; 

    constructor(cpf: string, uid: string, nome: string, urlFoto: string) { 
    this.cpf = cpf; 
    this.uid = uid; 
    this.nome = nome; 
    this.urlFoto = urlFoto; 
    } 
} 

Von dem, was ich bemerkt habe, wenn sie auf die moradores, aus irgendeinem Grund es nicht enthalten als array[] Mehrwert, , sondern als object{}

Dies ist ein Teil des Codes, der die Werte erhält:

private lotes: Lote[] = []; 
private ref: Observable<Lote[]>; 
ref.forEach(value => { 
     this.lotes = value; 
     }); 

Dies ist der Fehler eines ngular, wenn ich versuche zu machen ein *ngFor in moradores

Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

+0

Dieser Teil ist in Ordnung. Ich habe nicht den ganzen Code geschrieben, weil ich es nicht wichtig finde. Ich bekomme die Lose. nur dass Morador-Werte als ein Objekt und nicht als Liste kommen, wie es sein sollte. –

Antwort

0

Ich konnte das Problem lösen.

moradores war als Object kommen, also ging ich nur durch das Batch array und verwendet, um die Funktion von es6 Object.values() und array konvertieren.

Ich weiß nicht, ob mit try wie ich eine gute Praxis ist getan, aber es mein Problem gelöst, weil manchmal die moradores undefined oder null sein kann.

ref.forEach(value => { 
    this.lotes = value; 
    for (const lote of this.lotes) { 
    try { 
     lote.moradores = Object.values(lote.moradores); 
    } catch (err) { } 
    } 
});