NestJS 更改接收到的 POST 请求中的参数名称。

huangapple go评论77阅读模式
英文:

NestJS changing param names received in post request

问题

我希望一个端点能够接收JSON请求中的 firstName 并将其保存为我的数据库中的 first_name

我创建了一个DTO来概述我希望端点接收的参数,如下所示:

@IsNotEmpty()
@Expose({ name: 'firstName' })
first_name: string;

vscode智能感知片段

甚至在vscode中也能识别到属性名称应该是 first_name

然而,当我使用console.log时,我收到了这个(而不是 first_name):

{
  email: 'test@test.com',
  password: 'test',
  firstName: 'first',
  lastName: 'last'
}

(我的数据库实体也将该字段设置为 first_name,并且出现了错误)

英文:

My goal is for an endpoint to receive firstName in the JSON request and save first_name in my DB.

I created a dto outlining the params I want to receive for the endpoint like so:

@IsNotEmpty()
@Expose({ name: 'firstName' })
first_name: string;

vscode intellisense snip

vscode even picks up that the prop name should be first_name.

However when I console.log it I receive this: (firstName instead of first_name)

{
  email: 'test@test.com',
  password: 'test',
  firstName: 'first',
  lastName: 'last'
}

(My DB entity also has the field set up as first_name and it's throwing an error)

答案1

得分: 1

The validation pipe的transform: true选项需要设置,以便管道返回类实例而不仅仅是经过验证的JSON。

英文:

The validation pipe's transform: true option needs to be set so that the pipe returns the class instance rather than just the validated json

huangapple
  • 本文由 发表于 2023年1月6日 01:39:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75022347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定