typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.86k stars 503 forks source link

question: Unable to convert to plein object my classes+subclasses #635

Closed jaytonic closed 3 years ago

jaytonic commented 3 years ago

I was trying to... convert my typescript classes to plain javascript object to save them to firebase.

Here are my classes and my usage:

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();
  }
}

export class Itinerary {
  @Type(() => ItineraryStats)
  stats: ItineraryStats;
  @Type(() => Step, {
    discriminator: {
      property: '__type',
      subTypes: [
        { value: ActivityStep, name: 'ActivityStep' },
        { value: NightStep, name: 'NightStep' },
        { value: PointOfInterest, name: 'PointOfInterest' },
        { value: TravelStep, name: 'TravelStep' },
      ],
    },
  })
  steps: Step[];

  constructor() {
    this.steps = Step[0];
    this.stats = new ItineraryStats();
  }
}

export class ItineraryStats {
  ///In days
  duration: number;
  //in hours
  driveTime: number;
  //in kilometers
  driveDistance: number;

  averageDriveDistancePerDay(): number {
    return this.driveDistance / this.duration;
  }
  averageDriveTimePerDay(): number {
    return this.driveTime / this.duration;
  }
}

export abstract class Step {
  @Type(() => Date)
  start: Date;

  @Type(() => Date)
  end: Date;
  duration: number;
  enforcedStartStop: boolean;
  warning: string;
}

export class ActivityStep extends StopStep {
  duration: number;

  constructor() {
    super();
    this.id = Guid.newGuid();
  }
}

export class NightStep extends StopStep {
  nightNumber: number;
  numberOfNight: number;
}

export class PointOfInterest {
  @Type(()=>PointOfInterest)
  type: PointOfInterest;
}

export class TravelStep extends Step {
  @Type(() => TravelMode)
  Mode: TravelMode;
  PolyLines: string;
}

Here is how I'm trying to convert:

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)

The problem: But I'm getting an elusive error:

ERROR TypeError: Cannot read property 'constructor' of undefined
    at TransformOperationExecutor.js:207
    at Array.find (<anonymous>)
    at TransformOperationExecutor.transform (TransformOperationExecutor.js:207)
    at TransformOperationExecutor.transform (TransformOperationExecutor.js:266)
    at ClassTransformer.classToPlain (ClassTransformer.js:10)
    at classToPlain (index.js:8)
    at Object.toFirestore (trips.firestore.ts:16)
    at ma (index.cjs.js:15983)
    at n.set (index.cjs.js:15359)
    at AngularFirestoreDocument.set (angular-fire-firestore.js:512)

Here is a screenshot of my object:

image

What am I missing?

jaytonic commented 3 years ago

the issues was the undefined steps. solved it.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.