gpt4 book ai didi

node.js - Typescript - 使用 Class-transformer 扁平化 JSON 模型

转载 作者:行者123 更新时间:2023-12-05 00:55:21 31 4
gpt4 key购买 nike

我正在尝试在 typescript (node.js) 中使用类转换器: https://github.com/typestack/class-transformer

我想仅使用 1 个模型、1 个 Dto 将 JSON 扁平化,这将进入数据库

如果我有以下 json :

{
"data":{"test":"123"}
"name":"x"
}

DB 服务应该收到以下对象:

 {
"test":"123",
"name":"x"
}

这是我试图定义的,但它不起作用:

@Expose()
export class Dto{
name:string,
@Expose({name: "data.test"})
test: string
}

测试字段的结果未定义我怎样才能做到这一点?我不想创建“中介”模型

最佳答案

根据 doc制作

@Expose()
@Transform(({obj}) => obj.data.test)
test: string;

关于node.js - Typescript - 使用 Class-transformer 扁平化 JSON 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64738179/

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