gpt4 book ai didi

javascript - Nest Js TypeORM repository.update 方法不更改对象的多个字段

转载 作者:行者123 更新时间:2023-12-05 07:10:28 26 4
gpt4 key购买 nike

在我的 Nest js 项目中构建了一个端点,当用户提交旨在编辑现有组织的表单时,该端点会触发此服务功能:

async updateOrganization(
organization: UpdateOrganizationDto,
): Promise<Organization> {
this.organizationRepository.update(organization.organization.id, {
lastChangedBy: organization.profile.id,
orgName: organization.organization.orgName,
orgLead: organization.organization.orgLead,
orgAsst: organization.organization.orgAsst,
from: organization.organization.from,
to: organization.organization.to,
teamWideReporting: organization.organization.teamWideReporting,
leadsAddTeamMembers: organization.organization.leadsAddTeamMembers,
leadsRemoveTeamMembers: organization.organization.leadsRemoveTeamMembers,
});
return organization.organization;

用户可以在我们的 UI 上编辑 9 个可能的字段。每当用户点击我们的更新组织(在此处使用 TypeORM PUT 请求)时,我希望后端对所有可能的字段执行检查。

当我将此服务功能设置为仅更改一个字段时,发现一切正常,示例:

async updateOrganization(
organization: UpdateOrganizationDto,
): Promise<Organization> {
this.organizationRepository.update(organization.organization.id, {
orgName: organization.organization.orgName
});
return organization.organization;

但是,我想要一个一站式商店,以在对后端的单个请求中说明所有可能的更新。

我希望这只是一个简单的 typeOrm 语法问题,我们将不胜感激,谢谢!

最佳答案

确保等待从 .update() 返回的 promise :

async updateOrganization(
organization: UpdateOrganizationDto,
): Promise<Organization> {
// let the promise resolve
await this.organizationRepository.update(organization.organization.id, {
lastChangedBy: organization.profile.id,
orgName: organization.organization.orgName,
orgLead: organization.organization.orgLead,
orgAsst: organization.organization.orgAsst,
from: organization.organization.from,
to: organization.organization.to,
teamWideReporting: organization.organization.teamWideReporting,
leadsAddTeamMembers: organization.organization.leadsAddTeamMembers,
leadsRemoveTeamMembers: organization.organization.leadsRemoveTeamMembers,
});
return organization.organization;

它也可能是实体/模式的问题。您可以在 typeorm 配置(配置数据库连接的位置)中打开查询和错误日志记录: https://orkhan.gitbook.io/typeorm/docs/logging

{
host: "localhost",
...
logging: ["query", "error"]
}

如果您仍然遇到问题,请更新您与组织实体的帖子。

关于javascript - Nest Js TypeORM repository.update 方法不更改对象的多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61219924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com