Postman 如何使用TypeORM获取最后一个createdAt项
嗨,朋友们,我需要这个查询的帮助,我想通过“用户”只返回最后一个“案例” 根据我的邮递员请求,我的服务将返回用户提交的所有案例 users.services.tsPostman 如何使用TypeORM获取最后一个createdAt项,postman,nestjs,typeorm,Postman,Nestjs,Typeorm,嗨,朋友们,我需要这个查询的帮助,我想通过“用户”只返回最后一个“案例” 根据我的邮递员请求,我的服务将返回用户提交的所有案例 users.services.ts async findCasesbyUserId(id: number) { return await this.usersRepository.findOne(id, { relations: ['cases']}) } users.controller.ts @Get('/findCase/:id') as
async findCasesbyUserId(id: number) {
return await this.usersRepository.findOne(id, { relations: ['cases']})
}
users.controller.ts
@Get('/findCase/:id')
async findCasesByUserId(@Param('id', ParseIntPipe) id: number,) {
return this.usersService.findCasesbyUserId(id)
}
当我向'http://localhost:3000/users/findCase/3'. 这就是我得到的结果
id=3的用户拥有id=1、id=2、id=3的案例
所以我只想返回id=3的case,因为它是最后创建的case。我需要一种方法来获得最后一个案件的领域createdAt或其他方式可能是伟大的为我
我使用带有TypeORM的NestJs来查询我的PostgreSQL数据库
{
"id": 3,
"createdAt": "1617983079621",
"updatedAt": "1617983079621",
"name": "John",
"lastname": "Doe",
"email": "jdoe@email.com",
"username": "JohnDoe",
"password": "**************",
"role": "Admin",
"isActive": true,
"document": "12345",
"address": "Test",
"phone": "12345",
"cases": [
{
"id": 1,
"createdAt": "1618504530817",
"updatedAt": "1618504530817",
"message": "Test Case 1",
},
{
"id": 2,
"createdAt": "1618504530817",
"updatedAt": "1618504530817",
"message": "Test Case 1",
}, {
"id": 3,
"createdAt": "1618546146772",
"updatedAt": "1618546146772",
"message": "Test Case 1",
},
]
}
id
是指案例id还是指用户id?如果案例中的id是一个数字,您只需返回最大的一个,就不需要比较日期。