无法读取undefined的属性'unsubscribe'

问题描述 投票:1回答:1

我有一个离子项目。

我希望取消订阅,当我从订阅firebase返回城市。

但是当我尝试取消订阅时出现问题并且它会引发跟随错误。

怎么解决这个问题?

我的代码。

getCities() {
let i: any;
let a = this.firebaseProvider.getOtoparks()
  .subscribe(data => {
    for (i = 0; i < data.length; i++) {
      this.cities.push({
          name: data[i]['details']['sehir'],
          value: i,
        }
      );
    }
    // try to unsubscribe
    a.unsubscribe();
  });
}

enter image description here

firebase ionic-framework
1个回答
2
投票

最佳做法是在没有条件的情况下取消订阅时使用take(1)。

getCities() {
     let i: any;
     let a = this.firebaseProvider.getOtoparks()
         .take(1)
         .subscribe(data => {
             for (i = 0; i < data.length; i++) {
                 this.cities.push({
                     name: data[i]['details']['sehir'],
                     value: i,
                 });
             }
         });
}
© www.soinside.com 2019 - 2024. All rights reserved.