这里的问题是当我使用 plainToClass() 时,我的构造函数将抛出未定义构造函数参数的错误。
这是我的 Mongo de 查询,它返回 DeviceOperationsModel[] 数组。需要使用该库的辅助函数使用类转换器进行类型检查。
public async find(query: object): Promise<DeviceOperationsModel[]> {
this.appLoggerService.info(`Finding records on the basis of the following query: ${JSON.stringify(query)}`,
DeviceOperationsDbService.name);
return await this.deviceOperationsModel.find(query).lean().exec();
}
这是 DeviceOperationsModel 模型类。有嵌套的类对象
import { DeviceOperationsRequestModel } from './device-operations-request.model';
import { DeviceOperationsResponseModel } from './device-operations-response.model';
export class DeviceOperationsModel {
readonly correlationId: string;
readonly deviceId: string;
readonly requestType: string;
readonly request: DeviceOperationsRequestModel;
readonly response: DeviceOperationsResponseModel;
readonly createdAt: Date;
readonly systemName: string;
status: number;
statusDescription: string;
constructor(deviceOperationsData: any, correlationId: string, systemName: string, deviceOperationsResponseData?: any) {
this.correlationId = correlationId;
this.systemName = systemName;
this.deviceId = deviceOperationsData.deviceId;
this.requestType = deviceOperationsData.requestType;
this.request = deviceOperationsData;
this.response = deviceOperationsResponseData;
this.createdAt = deviceOperationsData.createdAt;
this.status = deviceOperationsData.status;
this.statusDescription = deviceOperationsData.statusDescription;
}
}
这是 deviceOperationsData 类定义。
import { DeviceOperationsDestinationBlobModel } from './device-operations-destination-blob.model';
export class DeviceOperationsRequestModel {
readonly serviceTimeoutInSeconds: number;
readonly agentTimeoutInSeconds: number;
readonly errorOnDisconnection: boolean;
readonly destinationBlob: DeviceOperationsDestinationBlobModel;
readonly metadata: object;
readonly agentRetryCount: number;
readonly agentRetryIntervalInSeconds: number;
readonly progressUpdate: boolean;
constructor(deviceOperation: any) {
this.serviceTimeoutInSeconds = deviceOperation.serviceTimeoutInSeconds;
this.agentTimeoutInSeconds = deviceOperation.agentTimeoutInSeconds;
this.errorOnDisconnection = deviceOperation.errorOnDisconnection;
this.destinationBlob = deviceOperation.destinationBlob;
this.metadata = deviceOperation.metadata;
this.agentRetryCount = deviceOperation.agentRetryCount;
this.agentRetryIntervalInSeconds = deviceOperation.agentRetryIntervalInSeconds;
this.progressUpdate = deviceOperation.progressUpdate;
}
}
等等。
在这里对 Mongo DB 数据进行类型转换的最佳方法是什么?
我试过了,但是
try {
const result = await this.deviceOperationsModel.find(query).lean().exec();
return plainToClass(DeviceOperationsModel, result as object[]);
} catch (error) {
console.log(error);
}
但是得到了相关性,设计,系统名称等是未定义的。