英文:
NestJS changing param names received in post request
问题
我希望一个端点能够接收JSON请求中的 firstName
并将其保存为我的数据库中的 first_name
。
我创建了一个DTO来概述我希望端点接收的参数,如下所示:
@IsNotEmpty()
@Expose({ name: 'firstName' })
first_name: string;
甚至在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 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论