gpt4 book ai didi

javascript - 如何使用类转换器将 typescript 类转换为纯 javascript 对象

转载 作者:行者123 更新时间:2023-12-04 12:56:22 38 4
gpt4 key购买 nike

我的 Angular 应用程序中有一些 typescript 类实例,我必须保存到 firebase。 Firebase 不支持自定义类,所以我发现了这个:https://github.com/typestack/class-transformer这似乎很适合我。
所以现在我正在创建我的对象,并尝试转换它。
这是我的对象。

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;
}
我正在创建一个“旅行”并尝试对其进行转换:
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)
但我明白了:
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)
我刚刚创建的对象:
enter image description here
我想要么我的对象处于不正确的状态,要么我的定义很差,但我找不到什么?

最佳答案

错误在于 Itinerary 的定义,更具体地说是代码行 this.steps = Step[0];它将变量设置为未定义,很可能不是故意的。 class-transformer图书馆可以处理属性(property)steps是一个空数组、步骤数组,或者一开始不存在但当值设置为未定义时崩溃。

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() {
// Step[0] causes the crash, because its undefined, either dont set it or
// set it to this.steps = []
this.steps = Step[0];
this.stats = new ItineraryStats();
}
}
更多详情:
由于在属性(property)上使用了鉴别器 steps相应的逻辑启动并比较 subTypes 下定义的构造函数,用当前值的构造函数找到正确的子类型名称:
    if (this.transformationType === TransformationType.CLASS_TO_PLAIN) {
subValue[targetType.options.discriminator.property] = targetType.options.discriminator.subTypes.find(
subType => subType.value === subValue.constructor
).name;
}
link to github Step[0]未定义最终使这条线 subValue.constructor碰撞。

关于javascript - 如何使用类转换器将 typescript 类转换为纯 javascript 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66553802/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com