hajisky
hajisky

Reputation: 873

error in angular2firebase snapshotchanges() subscription

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

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

and this is my 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;
});`

then I get this error on my chrome console

chrome

Upvotes: 2

Views: 480

Answers (1)

Jandro Rojas
Jandro Rojas

Reputation: 1972

I had the same problem and I solved it downgrading to a previous version. I'm now using firebase 4.12.1 and angularfire2 5.0.0-rc.6. I actually downgraded to a previous version of angular and RxJs as well as the new version introduced breaking changes and I would have to use an rxjs-compat package to make it work...it was a mess. I'll use these versions for now until all 3rd party libraries add support to the new version of rxjs. This is my package.json:

{
  "name": "support-portal",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "angularfire2": "^5.0.0-rc.6",
    "core-js": "^2.4.1",
    "firebase": "^4.12.1",
    "rxjs": "^5.5.2",
    "rxjs-compat": "^6.1.0",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^5.0.0",
    "@angular-devkit/build-angular": "~0.6.0",
    "typescript": "~2.4.2",
    "@angular/cli": "~6.0.0",
    "@angular/language-service": "^6.0.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }
}

I hope this gets you up and running, but it is a temporary solution, we should all be able to use the latest versions.

Upvotes: 1

Related Questions