sql - 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' }
怎么取值呢?
解决方案
已经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) {
...
}
}
推荐阅读
- docker - 在 Dockerfile 中使用哪个 docker 基础镜像?
- tizen - Tizen 三星智能电视 - 请在首选项中将日志记录级别设置为调试
- xsl-fo - 将单个页面拆分为 2 个垂直半 XSL-FO
- python - np.random.choice - 无法指定掷硬币的次数
- c# - `[Authorize (Roles="Role")]` 不起作用,User.IsInRole("Role") 始终为 false
- javascript - Createjs 到 PIXIJS 的转换
- c++ - 矢量通用参考
- angular-dart - Angular dart:如何在多级包环境中注入服务
- java - 如何在搜索功能中也将上升字母用作字母
- react-native - undefined 不是一个函数(评估 'react.default.createContext((0,_getTheme.default)())')