here the issue was when I was using plainToClass() my constructor will throw the error that the constructor parameter is not defined.
Here is my Mongo de query that returns the DeviceOperationsModel[] array. need to make the type check with class-transformer with the helper function of that library.
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();
}
Here is the DeviceOperationsModel model class. is have nested class object
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;
}
}
here is the deviceOperationsData class definition.
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;
}
}
and so on.
what is the best way to typecast of the Mongo DB data here?
I have tried but with
try {
const result = await this.deviceOperationsModel.find(query).lean().exec();
return plainToClass(DeviceOperationsModel, result as object[]);
} catch (error) {
console.log(error);
}
but got the correlation, devised, system name, etc are undefined.
In the example given in the class-transformer documentation, the class definition has no constructor. See here .
So, I suggest doing it with no constructor.
However, you seem to be expecting more data in the DeviceOperationsModel
constructor than just the object. plainToClass()
takes just an object and a class definition.
If you have optional properties that may not appear in the objects, mark them as optional in the class definition.
Also, you do not need to await
in the find
function. It returns a Promise, and you are not doing anything with the Promise result in the function. You don't even need to mark it async
(but maybe you were inspecting it for debugging earlier...).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.