简体   繁体   English

Angular2项目中的TSLint错误:无法将类型&#39;Subscription&#39;分配给类型&#39;FirebaseListObservable <any[]> “

[英]TSLint errors in Angular2 project: Type 'Subscription' is not assignable to type 'FirebaseListObservable<any[]>'

Having this issue, can't figure it out... 遇到这个问题,无法解决...

Here's the relevant code: 以下是相关代码:

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);
      }
    }
    );
  }

}

Errors: 错误:

TS2322: Type 'Subscription' is not assignable to type 'FirebaseListObservable'. TS2322:无法将类型“订阅”分配给类型“ FirebaseListObservable”。 Property '$ref' is missing in type 'Subscription'. “订阅”类型中缺少属性“ $ ref”。

How can I fix these? 我该如何解决? I'm new to both firebase and angular2. 我是Firebase和angular2的新手。 All suggestions are appreciated. 所有建议表示赞赏。

I tried using this (similar) question, but to no avail. 我尝试使用此(类似)问题,但无济于事。

Type 'Subscription' is not assignable to type 'Observable<SearchResult[]>' Angular 5 using HttpClient instead of Http 使用HttpClient而不是Http,不能将类型'Subscription'分配给类型'Observable <SearchResult []>'Angular 5

Thanks! 谢谢!

So following your stackBlitz I believe I have found your issue, without knowing what the data will look like at the end I can tell you what is going on. 因此,在您执行stackBlitz之后,我相信我已经找到了您的问题,但不知道最后的数据如何,我可以告诉您发生了什么。 First of all I suggest separating your logic as in the stackBlitz example, with a service. 首先,我建议使用服务将您的逻辑(如stackBlitz示例中)分开。

Whether you do or don't first of all make fbList into an Observable not a FireBaseObservableList - I believe it is deprecated anyway. 首先,不管您是否将fbList都设置为Observable而不是FireBaseObservableList,我相信无论如何它都已被弃用。 I also made items as an empty array of type any too (again I don't know what you want the end result to look like) but in anycase I didn't use the angular | 我也将项目也设置为空类型的数组(再次,我也不知道最终结果是什么样的),但是无论如何我都没有使用angular | async pipe) and add the .ValueChanges() to the fbList to make it an Observable. 异步管道),然后将.ValueChanges()添加到fbList使其可观察。

Alternatively if you know what your data will look like and want to use map then you do can declare items as a type of it then if you want to use the map operator, first import it to angular from rxjs/operators and remember you must use .pipe(map()) first. 另外,如果您知道数据的外观并希望使用map,则可以将项声明为数据的类型,然后,如果您要使用map运算符,请先从rxjs / operators将该数据导入angular并记住必须使用.pipe(map())首先。 Check out the example and try it without doing all the other stuff first to see it works. 查看该示例,并在不首先进行其他所有操作的情况下进行尝试,以查看其工作原理。 If you need more direction on the model please update a sample of the data. 如果您需要有关模型的更多指导,请更新数据样本。

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

暂无
暂无

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

相关问题 Angular2:类型'订阅'不能分配给类型 - Angular2 : Type 'Subscription' is not assignable to type 在angular2的服务层上修改对象:无法将订阅分配给可观察的类型 - Modifying object at service layer in angular2: Subscription is not assignable to the type observable 在“订阅”类型-angular2中缺少属性“包含” - Property 'includes' is missing in type 'Subscription' -angular2 类型“对象”不可分配给类型“NgIterable”<any> | null | 不明确的'。 在 Angular</any> - Type 'Object' is not assignable to type 'NgIterable<any> | null | undefined'. in Angular 在 Angular 4.x 应用程序中,类型“Object”不可分配给“any[]”类型 - Type 'Object' is not assignable to type 'any[]' in Angular 4.x app 无法将“订阅”类型分配给“可观察”类型 <Exercise[]> “ - Type 'Subscription' is not assignable to type 'Observable<Exercise[]>' “订阅”类型的参数不可分配给类型的参数 - Argument of type 'Subscription' is not assignable to parameter of type 角度2/4 | 类型“ {}”不可分配给类型“对象[]” - Angular 2/4 | Type '{}' is not assignable to type 'Object[]' angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM