首页 > 解决方案 > Nestjs typeorm (pgsql) 中的 UPDATE 是如何工作的?

问题描述

在 service.ts 文件中

......
import { Repository } from "typeorm";
....
constructor(
    @InjectRepository(Profile)
    private readonly profileRepository: Repository<Profile>,
  )......

async updateprofile(profile_id:string, data: any): Promise<any> {
    try {
      console.log(profile_id);
      await this.profileRepository.update(data); //how to update without saving new row in db

      return {
        success: true,
        message: 'Successfully updated profile',
      };
    } catch (err) {}}....

我不知道如何在不保存的情况下使用特定的 id 和正文数据进行更新。(此更新语法错误)因为使用保存时,它会生成两行具有唯一值的行,因此会出错。并且 console.log(id) 给出输出

{ profile_id: '29adb514-a10f-49c0-bde2-19dcbcf9e0b9' }

怎么取值呢?

标签: sqlsavenestjstypeorm

解决方案


已经profile_id是一个对象而不是一个字符串。

然后,要修改配置文件而不在数据库中添加一行,您只需执行以下操作:

import { Repository } from "typeorm";
....

constructor(
    @InjectRepository(Profile)
    private readonly profileRepository: Repository<Profile>,
  ){}

async updateprofile(dto: { profile_id: string }, data: any): Promise<any> {
    try {
      console.log(dto);
      await this.profileRepository.update(dto.profile_id, data); 

      return {
        success: true,
        message: 'Successfully updated profile',
      };
    } catch (err) {
      ...
    }
}


推荐阅读