I am working on an Angular 2 app with Typescript and I'm constantly having issues with the fact that when I cannot seem to persist object types very easily when syncing data.
I have been manually setting properties. In some cases I'm just removing the $exists(), $key, createdat functions and properties to be able to update the data.
Is there a way to set a class from a Firebase object without completely changing it?
For an example:
search$:FirebaseObjectObservable<ISearchQuery>;
search:ISearchQuery;
constructor(public af: AngularFire, public auth: AuthService){
this.search$ = this.af.database.object(`/queries/saved/${auth.id}`);
//THIS IS HOW I WANT TO DO IT
this.search$.subscribe((data)=>{
if(data.$exists()){
this.search=data;
}
});
//THIS IS WHAT I'VE RESORTED TO
this.search$.subscribe((data)=>{
if(data.$exists()){
this.search.categories = data.categories;
this.search.creators = data.creators;
this.search.endDate = data.endDate;
this.search.startDate = data.startDate;
this.search.location = data.location;
}
});
}
On the flip side when I've synced and updated data then I have to pick each property when updating or setting to firebase. I also run into issues if I sync directly I lose class functions as well (since firebase objects have their own set of functions in the prototype).
Is there a way to avoid picking property by property or a better way of dealing with syncing Firebase objects with Typescript?