首页 > 解决方案 > 更新到 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

没有运气

标签: nestjstypeorm

解决方案


我很确定 TypeORM 的save方法说它返回了一些东西,但不一定返回它。在这个示例中,我最终返回了在 TypeORM 调用之前创建的对象。在你的情况下,它会是

...
user.name = patchUser.name;
user.surname = patchUser.surmame;
...
await this._userREpository.save(user);
return user;

推荐阅读