NestJS GET 路由 returns 包裹在数据字段中的数据
NestJS GET route returns data wrapped in data field
如果有以下路由,应该return用户的用户名和城市。
控制器:
@HttpCode(200)
@Get(':userId')
async getUserUnauthenticated(@Param("userId") userId: string): Promise<GetUserDto> {
return this.usersService.getUsers(userId, false)
}
服务:
async getUsers(userId: string, authenticated: boolean): Promise<GetUserDto> {
const user = await this.fusionAuthService.searchUserById(userId);
const impacter = await this.impactersService.retrieveImpacterFromDatabaseById(userId)
if (authenticated) {
return {
username: user.username,
city: impacter.address.city,
image: user.imageUrl
}
} else {
return {
username: user.username,
city: impacter.address.city
}
}
}
DTO:
export class GetUserDto {
username: string;
city: string;
image?: string;
}
但不是
{
"username": "ExampleName",
"city": "ExampleTown"
}
它returns
{
"data": {
"username": "ExampleName",
"city": "ExampleTown"
}
}
我做错了什么?
看起来你有一些拦截器可以转换你的输出,看看这里它是如何工作的,https://docs.nestjs.com/interceptors也许你能在你的项目中弄清楚它是如何工作的。
此致。
如果有以下路由,应该return用户的用户名和城市。
控制器:
@HttpCode(200)
@Get(':userId')
async getUserUnauthenticated(@Param("userId") userId: string): Promise<GetUserDto> {
return this.usersService.getUsers(userId, false)
}
服务:
async getUsers(userId: string, authenticated: boolean): Promise<GetUserDto> {
const user = await this.fusionAuthService.searchUserById(userId);
const impacter = await this.impactersService.retrieveImpacterFromDatabaseById(userId)
if (authenticated) {
return {
username: user.username,
city: impacter.address.city,
image: user.imageUrl
}
} else {
return {
username: user.username,
city: impacter.address.city
}
}
}
DTO:
export class GetUserDto {
username: string;
city: string;
image?: string;
}
但不是
{
"username": "ExampleName",
"city": "ExampleTown"
}
它returns
{
"data": {
"username": "ExampleName",
"city": "ExampleTown"
}
}
我做错了什么?
看起来你有一些拦截器可以转换你的输出,看看这里它是如何工作的,https://docs.nestjs.com/interceptors也许你能在你的项目中弄清楚它是如何工作的。
此致。