类型“ Observable <DocumentChangeAction <事件> []>”上不存在属性“地图”

时间:2020-04-16 09:23:10

标签: angular firebase rxjs angular9 rxjs-observables

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

类型“可观察[]>”上不存在属性“地图”。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您的RxJ版本是什么?从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;
            });
        })
    );