nestjs - 更新到 postgress 后,nestjs 返回空对象
问题描述
我最近遇到一个问题...
一旦我与 postgress 数据库交互并要求返回对象 n 得到一个空白{}
,我不确定我是否 getUsers 它返回一个数组,其中包含数组中的对象。
这是我的参考代码:
public async updateUserById(patchUser: CreateUserDto, userId: number, profile_image: any) {
const user = await this._userRepository.createQueryBuilder('user')
.where('user.id = :id', {id: userId})
.getOne()
if(!user) {
throw new InternalServerErrorException(`User with id ${userId} does not exist`);
}
user.name = patchUser.name;
user.surname = patchUser.surname;
user.profile_image = profile_image.location;
user.email = patchUser.email;
return await this._userRepository.save(user);
}
我也试过:
const foundUser = await this._userRepository.save(user);
return foundUser
没有运气
解决方案
我很确定 TypeORM 的save
方法说它返回了一些东西,但不一定返回它。在这个示例中,我最终返回了在 TypeORM 调用之前创建的对象。在你的情况下,它会是
...
user.name = patchUser.name;
user.surname = patchUser.surmame;
...
await this._userREpository.save(user);
return user;
推荐阅读
- visual-studio-code - 更改 ctrl+ko(新项目)目录
- java - 如何在 Launch4j 创建的 .exe 文件中打包 JavaFX?
- javascript - 修复 Redux 有效负载正在控制台中登录减速器,但在模式中说无法读取 null 的属性?
- excel - 用户表单应该只返回过滤的行
- angular - Angular 7 调用服务,承诺在调用另一个函数的组件内调用函数
- spring-webflux - 我怎样才能 bodyToMono 两次?
- python - 遍历 json 对象,然后将结果添加到数组中
- python - 如何修复 VSCODE 中的自动完成问题?
- jmeter - JMeter - 移动设备请求使用 JSR223 和 groovy 进行仿真 - 没有这样的属性:ExpectedConditions 错误
- math - 从初始值重新计算cropdetect参数