属性 'map' 在类型 'Observable<DocumentChangeAction<Event>[]>' 上不存在

Property 'map' does not exist on type 'Observable<DocumentChangeAction<Event>[]>'

我的服务文件中出现以下错误,建议更改代码:-

属性 'map' 在类型 'Observable[]>' 上不存在。

我的 Event.ts 包含一个带有 5 个声明参数的接口,它们是 id: string;代码:字符串;名称:字符串;密码:字符串; pollCat:字符串;

events.service.ts:-

import { Injectable } from '@angular/core';
    import { AngularFirestore , AngularFirestoreCollection, AngularFirestoreDocument} from 'angularfire2/firestore';
    import { Event } from '../models/Event';
    import { Observable } from 'rxjs';
    import {map} from 'rxjs/operators';


    @Injectable({
      providedIn: 'root'
    })
    export class EventsService {

      eventsCollection : AngularFirestoreCollection<Event>;
      events: Observable<Event[]>;

      constructor(public afs: AngularFirestore) { 
        this.events = this.afs.collection<Event>('Events').snapshotChanges().map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data() as Event;
            data.id = a.payload.doc.id;
            return data;
          })
        });
      }

      getEvents()
      {
        return this.events;
      }
    }

events.component.ts:-

import { Component, OnInit } from '@angular/core';
import { EventsService } from 'src/app/services/events.service';
import { Event } from '../../models/Event';


@Component({
  selector: 'app-events',
  templateUrl: './events.component.html',
  styleUrls: ['./events.component.css']
})
export class EventsComponent implements OnInit {

  events: Event[];

  constructor(public eventsService: EventsService) { }

  ngOnInit() {
    this.eventsService.getEvents().subscribe(events => {
      this.events = events;
      console.log(events);
    });
  }
}

我们不能像这样使用 RXJS 运算符,我们需要将它们包含在 pipe 方法中。请进行以下更改。

      constructor(public afs: AngularFirestore) { 
        this.events = this.afs.collection<Event>('Events').snapshotChanges().pipe(
map(changes => {
          return changes.map(a => {
            const data = a.payload.doc.data() as Event;
            data.id = a.payload.doc.id;
            return data;
          })
        }));
      }

你的 RxJs 是什么版本?从 5.5 版开始,您需要使用 pipe :

this.events = this.afs.collection<Event ('Events').snapshotChanges()
    .pipe(
        map(changes => {
            return changes.map(a => {
                const data = a.payload.doc.data() as Event;
                data.id = a.payload.doc.id;
                return data;
            });
        })
    );