【问题标题】:Angular: Deletion of two separate interdependent modelsAngular:删除两个独立的相互依赖的模型
【发布时间】:2022-07-11 20:45:00
【问题描述】:

我有 2 个用户和联系人模型 一旦用户被删除,我想要的表之间就没有连接,那么用它创建的相同的联系人姓名也将被删除

例如:

我在 SignUp 中所做的操作会同时添加一个新联系人和一个新用户,但是 在删除中我不知道如何捕捉它们,因为它们没有相互连接。

添加工作好(添加联系人和用户):

signUp(){
    this.usersService.Register(this.signupForm.value)
    .subscribe(() => {
    this.contactsService.AddContact(this.signupForm.value)
    .subscribe(() => {
      this.swal.success();
      this.signupForm.reset();
      this.router.navigate(['login']);
    })
    },(e)=> {
      this.errorsService.errors(e);
    })
  }

删除:(无效):

this.contactsService.DeleteContact(contact.id)
    .subscribe(() =>{
      this.router.navigate(['login']);
    })

    this.usersService.DeleteUser(user.id)
    .subscribe(()=>{
        this.router.navigate(['login']);
      })
    }

在删除过程中,他只删除了用户而不是联系人,因为导航是根据用户的 id 完成的

【问题讨论】:

  • 您应该在联系人模型中添加一个可索引的“userId”字段,因此每当您使用用户删除时,您也可以找到删除联系人的信息。
  • 这是我做不到的问题,模型不应该互相认识
  • 为什么?如果是这样,您不能将这两个信息都保存在另一个模型中,然后改用它吗?
  • 因为是微服务

标签: angular


【解决方案1】:

您应该在联系人模型中添加一个可索引的字段“userId”,因此每当您使用用户删除时,您也可以找到删除联系人的信息。

Contact {
   userId: string // This relates to the user.id you get
}

这也意味着您应该在保存用户后保存联系人(使用该 ID,用于 db 关系)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2011-05-03
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    相关资源
    最近更新 更多