I have a few instances of TypeScript classes in my Angular app that I need to save to Firebase. However, Firebase does not support custom classes, so I stumbled upon this library: https://github.com/typestack/class-transformer which seems to be a good fit for my needs.
Now, I am working on creating my object and trying to convert it.
Here is an example of my object:
export class Trip {
id: string;
owner: string;
name: string;
@Type(() => Date)
startDate: Date;
@Type(() => Date)
endDate: Date | undefined;
coverImageUrl: string;
@Type(() => Itinerary)
itinerary: Itinerary;
constructor() {
this.itinerary = new Itinerary();
}
}
//other class definitions...
I am currently creating a "trip" instance and attempting to convert it:
const trip = new Trip();
trip.owner= firebase.auth().currentUser.uid;
trip.name= action.name;
trip.startDate = action.startDate,
trip.endDate = action.endDate;
console.log(trip);
const converted = classToPlain(trip)
However, when I try to convert it, I encounter the following error:
ERROR TypeError: Cannot read the property 'constructor' of undefined
// stack trace continues...
The object I just created: https://i.sstatic.net/GG3FC.png
I suspect that either my object is in an incorrect state or there is something poorly defined, but I'm having trouble pinpointing the issue.