簡體   English   中英

我們需要借助類轉換器將 JS object 反序列化為 Model 模式

[英]We need to deserialize in JS object to Model schema with help of class-transformer

這里的問題是當我使用 plainToClass() 時,我的構造函數將拋出未定義構造函數參數的錯誤。

這是我的 Mongo de 查詢,它返回 DeviceOperationsModel[] 數組。 需要使用該庫的幫助器 function 使用類轉換器進行類型檢查。

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 model class。 有嵌套的 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;
    }
}

這是 deviceOperationsData class 定義。

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

但是得到了相關性,設計,系統名稱等是未定義的。

在類轉換器文檔中給出的示例中,class 定義沒有構造函數。 這里

所以,我建議在沒有構造函數的情況下這樣做。

但是,您似乎期望DeviceOperationsModel構造函數中的數據不僅僅是 object。 plainToClass()只需要一個 object 和一個 class 定義。

如果您有可能不會出現在對象中的可選屬性,請在 class 定義中將它們標記為可選。

此外,您無需await find function。 它返回 Promise,而您沒有對 Promise 結果做任何事情,結果是 function。 您甚至不需要將其標記為async (但也許您之前正在檢查它以進行調試......)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM