【问题标题】:We need to deserialize in JS object to Model schema with help of class-transformer我们需要借助类转换器将 JS 对象反序列化为模型模式
【发布时间】:2020-11-13 22:52:44
【问题描述】:

这里的问题是,当我使用 plainToClass() 时,我的构造函数会抛出未定义构造函数参数的错误。

这是我的 Mongo de 查询,它返回 DeviceOperationsModel[] 数组。需要使用该库的辅助函数使用 class-transformer 进行类型检查。

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

但是得到了相关性,设计,系统名称等是未定义的。

【问题讨论】:

    标签: javascript node.js typescript nestjs class-transformer


    【解决方案1】:

    在类转换器文档中给出的示例中,类定义没有构造函数。见here

    所以,我建议不使用构造函数。

    但是,您似乎期望DeviceOperationsModel 构造函数中的数据不仅仅是对象。 plainToClass() 只接受一个对象和一个类定义。

    如果您有可能不会出现在对象中的可选属性,请在类定义中将它们标记为可选。

    另外,您不需要在find 函数中使用await。它返回一个 Promise,并且您没有对函数中的 Promise 结果做任何事情。您甚至不需要标记它async(但也许您之前正在检查它以进行调试......)。

    【讨论】:

    • 我试过了,但是用 try { const result = await this.deviceOperationsModel.find(query).lean().exec(); return plainToClass(DeviceOperationsModel, result as object[]); } 捕捉(错误){ console.log(错误); } 但得到了相关性、设计、系统名称等未定义。
    • 查看我的编辑。在类定义中将它们标记为可选。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2021-02-13
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2010-11-30
    • 2020-01-26
    相关资源
    最近更新 更多