Angular2项目中的TSLint错误:无法将类型'Subscription'分配为类型'FirebaseListObservable <any []>'

时间:2018-10-22 22:07:17

标签: javascript angular firebase eslint tslint

遇到这个问题,无法解决...

以下是相关代码:

export class AdminComponent {
  items: FirebaseListObservable < any[] > ;
  private xyz: string;
  private fbList: FirebaseListObservable < any[] > ;
  private userData: object[];
  private userEid: object[];
  private userFullName: object[];
  private uniqueID: string;
  private quizAnswerA: string;

  private userList = [];
  private userAnswer1: any;

  constructor(db: AngularFireDatabase, private router: Router, private route: ActivatedRoute) {
    this.fbList = db.list('/');

    this.items = this.fbList.map(items => {
      const dbAnalytics = items[0];
      const dbBookings = items[1];
      const dbClasses = items[2];
      const dbLabs = items[3];
      const dbMachines = items[4];
      const dbPostLabs = items[5];
      const dbPreLabs = items[6];
      const dbUsers = items[7];

      for (const key of Object.keys(dbUsers)) {
        if (dbUsers.hasOwnProperty(key)) {
          this.userData = dbUsers[key];
          this.uniqueID = key;
          this.userEid = this.userData['eid'] !== undefined ? this.userData['eid'] : 'N/A';
          this.userFullName = this.userData['name'];

        }
        let time = 'N/A';
        let count = 0;
        let runs = dbAnalytics['expRuns'];
        if ((key in runs)) {
          let experiments = runs[key]['-KVQN-Qk-CLh_KupgKTt'];
          count = Object.keys(experiments)
            .length;
          try {
            time = experiments[Object.keys(experiments)[Object.keys(experiments)
              .length - 1]]['time'].replace('T', '  ')
              .split('.')[0];
          } catch (e) {}

        }

        let firstQuizAnswer = dbPostLabs['-KVQN-Qk-CLh_KupgKTt']['a']['completed'];
        let secondQuizAnswer = dbPostLabs['-KVQN-Qk-CLh_KupgKTt']['b']['completed'];
        let thirdQuizAnswer = dbPostLabs['-KVQN-Qk-CLh_KupgKTt']['c']['completed'];
        let fourthQuizAnswer = dbPostLabs['-KVQN-Qk-CLh_KupgKTt']['d']['completed'];

        let store = new Row(this.userFullName, this.userEid, this.uniqueID, count, time, firstQuizAnswer[key], secondQuizAnswer[key], thirdQuizAnswer[key], fourthQuizAnswer[key]);
        this.userList.push(store);
      }
    }
    );
  }

}

错误:

  

TS2322:无法将'Subscription'类型分配给'FirebaseListObservable'类型。属性'$ ref'在'订阅'类型中丢失。

如何解决这些问题?我是Firebase和angular2的新手。所有建议表示赞赏。

我尝试使用此(类似)问题,但无济于事。

Type 'Subscription' is not assignable to type 'Observable<SearchResult[]>' Angular 5 using HttpClient instead of Http

谢谢!

1 个答案:

答案 0 :(得分:0)

因此,在您的stackBlitz之后,我相信我已经找到了您的问题,但不知道最后的数据如何,我可以告诉您发生了什么。首先,我建议使用服务将您的逻辑(如stackBlitz示例中)分开。

您是否首先将fbList设置为Observable而不是FireBaseObservableList-我相信无论如何它都已被弃用。我也将项目也设置为空类型的数组(再次,我也不知道最终结果是什么样的),但是无论如何我都没有使用angular |异步管道),然后将.ValueChanges()添加到fbList使其可观察。

或者,如果您知道数据的外观并希望使用map,则可以将项声明为数据的类型,然后,如果您要使用map运算符,请先将其从rxjs / operators导入angular并记住您必须先使用.pipe(map())。查看该示例,并在不首先进行其他所有操作的情况下进行尝试,以查看其工作原理。如果您需要有关模型的更多指导,请更新数据样本。

https://angular-rt-database-dmw7gd.stackblitz.io