2017-12-05 3 views
0

Ich habe einen Service, wo ich "Momente" aus meinem Backend abrufen. Ich habe 2 Funktionen. Eine Methode get(), die 1 Objekt und eine Methode search() zurückgibt, die ein Array von Objekten zurückgibt.Angular 5 + Typescript - Rückgabe Array von neu erstellten typisierten Objekten

moment.service.ts

In der get-Methode, Karte i die res aus dem Backend eine neue Instanz meines Moment Klasse zu erstellen. Was gut funktioniert.

get(moment_id) { 

     let endpoint = this.path + moment_id; 

     return this.apiService.get(endpoint) 
           .map((res) => new Moment(res.data)); 
    } 

Ich möchte das gleiche tun, außer in der Suchmethode. Es sollte das Array so abbilden, dass alle Objekte darin eine neue Instanz der Momentklasse sind.

search(filters) { 

    let endpoint = this.path + 'search'; 

    let params = new HttpParams({ fromObject: filters }); 

    return this.apiService.get(endpoint, params) 
          .map((res) => new Array<Moment>(res)); 
} 

Dies funktioniert jedoch nicht das zurückgegebene Array zeigt nicht, dass die Objekte vom Typ Moment sind.

enter image description here

moment.component.ts

moments: Moment[] = []; 

    this.momentService.search(filters).subscribe((res) => { 
       this.moments = res; 
       console.log(this.moments); 
      }); 

moment.model.ts

import { Comment } from './comment.model'; 
import { User } from './user.model'; 

export class Moment { 

    _id?: string = null; 
    body?: string = null; 
    author?: User = null; 
    likes?: any[] = []; 
    dislikes?: any[] = []; 
    comments?: Comment[] = []; 
    created_at?: string = null; 
    updated_at?: string = null; 


    constructor(data?: Moment) { 
     if (data) { 
      this.deserialize(data); 
     } 
    } 

    private deserialize(data: Moment) { 

     const keys = Object.keys(this); 
     for (const key of keys) { 
      if (key === 'author') { 
       this[key] = new User(data['author']); 
      } else if (key === 'comments') { 
       this[key] = data['comments'].map(c => new Comment(c)); 
      } else { 
       this[key] = data[key]; 
      } 
     } 
    } 
} 

Antwort

1

Versuchen Momente Instanziierung wie folgt:

search(filters) { 

    let endpoint = this.path + 'search'; 

    let params = new HttpParams({ fromObject: filters }); 

    return this.apiService.get(endpoint, params) 
          .map((res) => new Array<Moment>(new Moment(res))); 
} 
+0

Verdammt, ich sah das so lange nicht. Allerdings bekomme ich einen Folgefehler, der nicht mit der einfachen Get-Funktion kommt. Der Fehler lautet: "Kann die Property-Map von undefined nicht lesen" in meiner .model.ts auf "this [key] = data ['comments']. Map (c => new Comment (c));" irgendwelche Ideen? – KHAN

+0

Ich habe gerade festgestellt, dass die Res vom Typ Array ist, die Objekte enthält. Ich denke, ich muss einzelne Objekte innerhalb der res [] in neuen Moment senden. – KHAN

+0

So endete ich mit diesem '' 'zurück this.apiService.get (Endpunkt, Parameter) .map ((res) => res.map ((x) => neuer Moment (x)));' '' weil auch ein neues Array ein Array um ein Array herum erstellt hat. Ein bisschen seltsam, dass ich 2 Karten tho mache. – KHAN

Verwandte Themen