Skip to content

SnapshotChanges subscription error #1611

@hajiskyy

Description

@hajiskyy

Version info

**Angular:5.2.0

**Firebase:^5.0.1

**AngularFire2: ^5.0.0-rc.7

Problem

I get a changes.forEach error when trying to get data from firebase using snapshotChanges() but it works fine if i use valuechanges(). I'm not sure what i'm doing wrong so pls help

employee service

`import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from "angularfire2/firestore";
import { BehaviorSubject } from "rxjs/BehaviorSubject";
import { of } from "rxjs/Observable/of";
import {Observable} from 'rxjs';
import 'rxjs/add/operator/map';
import { Employee } from "../models/employee";

@Injectable()
export class EmployeeService {
employees: Observable<Employee[]>;
employeeCollection: AngularFirestoreCollection;

constructor( public db: AngularFirestore ) {
db.firestore.settings({ timestampsInSnapshots: true });
this.employeeCollection = this.db.collection('employee');
this.employees = this.employeeCollection.snapshotChanges().map(changes => {
return changes.map(a => {
const data = a.payload.doc.data() as Employee;
data.id = a.payload.doc.id;
return data;
});
});
}

getEmployees(){
return this.employees;
}`

employee component

` import { Component, OnInit } from '@angular/core';
import { EmployeeService } from "../../services/employee.service";
import { Employee } from "../../models/employee";

@component({
selector: 'app-employees',
templateUrl: './employees.component.html',
styleUrls: ['./employees.component.css']
})
export class EmployeesComponent implements OnInit {
employees: Employee[];
constructor(
private employeeService: EmployeeService
) { }

ngOnInit() {
this.employeeService.getEmployees().subscribe(employees => {
this.employees = employees;
});`

Expected behavior

Employee data is fetched

Actual behavior

output on chrome console
Uncaught TypeError: changes.forEach is not a function
at combineChanges (changes.js:19)
at ScanSubscriber.eval [as accumulator] (changes.js:15)
at ScanSubscriber._tryNext (scan.js:114)
at ScanSubscriber._next (scan.js:107)
at ScanSubscriber.Subscriber.next (Subscriber.js:95)
at MapSubscriber._next (map.js:85)
at MapSubscriber.Subscriber.next (Subscriber.js:95)
at MapSubscriber._next (map.js:85)
at MapSubscriber.Subscriber.next (Subscriber.js:95)
at RefCountSubscriber.Subscriber._next (Subscriber.js:131)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions