Angular NGXS获取对象的id

Angular NGXS获取对象的id,angular,ngxs,Angular,Ngxs,我正在编写一个应用程序,通过使用注入了serverService的ngxs,我可以在mongodb中保存事件。当我发送事件时,我不需要id,因为mongo会自动添加id,只需等待它,然后修补状态。(insertEvent returng id然后我查找整个事件并获取它) 当我尝试使用动作处理时,它总是返回带有mock id的事件 this.actions$.pipe(ofActionSuccessful(CreateEvent)).subscribe((ev) => { consol

我正在编写一个应用程序,通过使用注入了serverService的ngxs,我可以在mongodb中保存事件。当我发送事件时,我不需要id,因为mongo会自动添加id,只需等待它,然后修补状态。(insertEvent returng id然后我查找整个事件并获取它)

当我尝试使用动作处理时,它总是返回带有mock id的事件

this.actions$.pipe(ofActionSuccessful(CreateEvent)).subscribe((ev) => {
   console.log(ev)
})

是的,本例中的操作流将返回您调度的操作的有效负载(带有模拟ID)

我可以在这里看到两种选择:

  • 创建一个不同的操作,例如,
    EventCreated
    ,然后您可以订阅该操作-在此操作负载中,包括刚刚创建的内容的详细信息(ID)。在您的
    修补程序
    操作后分派它
  • 在当前操作流管道中,使用
    withLatestFrom
    操作符获取状态的最新视图,并检查`事件数组中的最后一个
    事件
    ,以查看生成了什么ID
  • createRandom(){
        this.store.dispatch(
          new CreateEvent(EventFactory.create())  
        )
      }
    
    this.actions$.pipe(ofActionSuccessful(CreateEvent)).subscribe((ev) => {
       console.log(ev)
    })