简体   繁体   English

Angular - 管道从其中包含条件的方法调用返回可观察的 - 无法读取未定义的属性“订阅”

[英]Angular - Piping returned observable from method call which has a conditional in it - Cannot read property 'subscribe' of undefined

I am trying to pipe the returned observable from a save method for a component.我正在尝试 pipe 从组件的保存方法返回的 observable。

Inside this save method based on a condition a dialog is opened which waits for a user input before making the call to the update endpoint and returning the result as an observable which is then ultimately returned by the save() method.在此保存方法中,基于条件打开一个对话框,该对话框在调用更新端点之前等待用户输入,并将结果作为可观察对象返回,然后最终由 save() 方法返回。

The issue is that I am trying to pipe the result of that save() to check the value has been emitted and then navigate away from the component.问题是我正在尝试 pipe 该 save() 的结果以检查该值是否已发出,然后导航离开该组件。 As the.subscribe() is executing before the value has been returned.由于 .subscribe() 在返回值之前执行。

A summary of the code looks like this:代码摘要如下所示:

save() : Observable<Address> {
let value = this.form.get('Address').value
if (JSON.stringify(this.address) != JSON.stringify(value.Address)) {
  const ref = this.dialog.open(AddressChangeDialog);
  ref.componentInstance.canSave = this.form.valid;
  ref.afterClosed().pipe(map(result => {
    if (result) {
      switch (result) {
        case AddressChangeDialog.Save:
          //Save the form (returns the observable here)
          return this.addressService.put(value)
        case AddressChangeDialog.Discard:
          // Cancel the save
          break;
        default:
          break;
      }
    }
  }));
}
else {
  // if the address hasnt changed just save and return the observable
  return this.addressService.put(value)
 }
}

This is then called by another method然后由另一种方法调用

  onSaveButtonClick() {
    this.save().subscribe();
  }

The problem I am having is that when the address has changed and it opens the dialog I get an error on the.subscribe() due to the fact that the save() method hasnt returned anything yet.我遇到的问题是,当地址更改并打开对话框时,由于 save() 方法尚未返回任何内容,我在 the.subscribe() 上收到错误。

Any help will be much appreciated as Ive been scratching my head for a while now.任何帮助都将不胜感激,因为我现在已经摸不着头脑了。

Thanks!谢谢!

When using map() you always have to return something.使用map()时,您总是需要返回一些东西。 There are several cases when you don't return anything.有几种情况您不返回任何东西。 Return null at least.至少返回null

save() : Observable<Address> {
    let value = this.form.get('Address').value

    if (JSON.stringify(this.address) != JSON.stringify(value.Address)) {
        const ref = this.dialog.open(AddressChangeDialog);
        ref.componentInstance.canSave = this.form.valid;

        ref.afterClosed().pipe(map(result => {

            if (result) {
 
               switch (result) {
                   case AddressChangeDialog.Save: 
                   //Save the form (returns the observable here)
                   return this.addressService.put(value)
    
                   case AddressChangeDialog.Discard:
                   // Cancel the save

                   // no return value
                   break;

                   // no return value
                   default:
                   break;
               }

            }

            // no return value
            // You always have to return something. So here we
            // return null in case no other return is called before
            return null;
        }));
    }
    else {
        // if the address hasnt changed just save and return the observable
        return this.addressService.put(value)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 9 测试组件依赖于可观察的服务 - 无法读取未定义的属性“订阅” - Angular 9 testing component dependant on service with observable - Cannot read property 'subscribe' of undefined 错误类型错误:无法读取 POST HTTP 调用中未定义的属性“订阅” - Angular [8] - ERROR TypeError: Cannot read property 'subscribe' of undefined on POST HTTP Call - Angular [8] 为什么我不能在订阅中调用方法 - 无法读取未定义的属性“showMessageFromSocket” - Why i can't call method in subscribe - Cannot read property 'showMessageFromSocket' of undefined rxjs可观察-订阅导致循环并在条件上中断/无法读取未定义的属性&#39;subscribe&#39; - rxjs Observable - subscribe to result in loop and break on condition / cannot read property 'subscribe' of undefined 角度单元测试TypeError:无法读取未定义的属性“订阅” - Angular unit testing TypeError: Cannot read property 'subscribe' of undefined 对KO Observable Array的Foursquare调用:无法读取未定义的属性“ push” - Foursquare call to KO Observable Array: Cannot read property 'push' of undefined 角5-可观察到的返回错误无法读取未定义的属性 - Angular 5 - Observable return error cannot read property of undefined 上传图片,无法读取未定义的属性“订阅” - Upload image, Cannot read property 'subscribe' of undefined 无法读取未定义TypeError的属性“ subscribe” - Cannot read property 'subscribe' of undefined TypeError 无法读取未定义的属性“has” - Cannot read property 'has' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM