在typeorm express中多对多更新数据
Update data many-to-many in typeorm express
我尝试了 .save 和 .update 但它会导致错误,--在 的情况下.save it return 错误:重复键的值打破了唯一约束 --in .update如果它 return 错误:“用户”关系的“userId”列不存在
注意:我有 2 个表角色和用户以及关联 user_role
//Many-to-many relation with user
@ManyToMany((type) => User, (user) => user.roles)
users: User[];
//Many-to-many relation with role
@ManyToMany((type) => Role, {
cascade: true,
})
@JoinTable({
name: "users_roles",
joinColumn: { name: "userId", referencedColumnName: "id" },
inverseJoinColumn: { name: "roleId" }
})
roles: Role[];
源代码是:
/* 数据示例
实体2 = {
用户名:'user8',
密码:'user8',
名字:'test',
姓氏:'tt',
电子邮件:'user8@gmail.com',
公司:18,
角色:[ { id: 62 }, { id: 63 } ]
} */
let entity = await this.userRepository.create(data.payload);
let entity2 = { ...entity, roles: data.payload.selectedRoles }
const user = await this.userRepository.save(entity2);
/*const user = await this.userRepository.update(id, entity2);*/
//todo we need to serialize the user
// return only what we need like username , email, ...etc not password!!!!
return { success: true, user: SanitizeUser(user) };
我确实找到了,然后保存,首先使用 data= this.userRepository.findOne(id) 获取要更新的数据,然后您可以将 userRepository.save(data) 应用于此数据。例如:
const userUpdated = await this.userRepository.findOne(id)
let dataUpdated = {
...userUpdated,
username: data.payload.username,
firstname: data.payload.firstname,
lastname: data.payload.lastname,
email: data.payload.email,
company: data.payload.selectedCompany,
roles: data.payload.selectedRoles
}
const user = await this.userRepository.save(dataUpdated);
我尝试了 .save 和 .update 但它会导致错误,--在 的情况下.save it return 错误:重复键的值打破了唯一约束 --in .update如果它 return 错误:“用户”关系的“userId”列不存在
注意:我有 2 个表角色和用户以及关联 user_role
//Many-to-many relation with user
@ManyToMany((type) => User, (user) => user.roles)
users: User[];
//Many-to-many relation with role
@ManyToMany((type) => Role, {
cascade: true,
})
@JoinTable({
name: "users_roles",
joinColumn: { name: "userId", referencedColumnName: "id" },
inverseJoinColumn: { name: "roleId" }
})
roles: Role[];
源代码是: /* 数据示例 实体2 = { 用户名:'user8', 密码:'user8', 名字:'test', 姓氏:'tt', 电子邮件:'user8@gmail.com', 公司:18, 角色:[ { id: 62 }, { id: 63 } ] } */
let entity = await this.userRepository.create(data.payload);
let entity2 = { ...entity, roles: data.payload.selectedRoles }
const user = await this.userRepository.save(entity2);
/*const user = await this.userRepository.update(id, entity2);*/
//todo we need to serialize the user
// return only what we need like username , email, ...etc not password!!!!
return { success: true, user: SanitizeUser(user) };
我确实找到了,然后保存,首先使用 data= this.userRepository.findOne(id) 获取要更新的数据,然后您可以将 userRepository.save(data) 应用于此数据。例如:
const userUpdated = await this.userRepository.findOne(id)
let dataUpdated = {
...userUpdated,
username: data.payload.username,
firstname: data.payload.firstname,
lastname: data.payload.lastname,
email: data.payload.email,
company: data.payload.selectedCompany,
roles: data.payload.selectedRoles
}
const user = await this.userRepository.save(dataUpdated);