question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AngularFire as a service (Not finding any documentation "On The Line...")

See original GitHub issue

I have been building an App using the latest releases and Angular and AngularFire. When using the documentation provided, I can do all the CRUD operations just as prescribed in the Docs. However, I am having a difficult time understanding how to convert it to a service.

For example: Let’s say I have a collection of crew members. There may be several components that need to consume the collection data containing all info about the crew members. It clearly makes sense to do this using a service and not do this massive chunk of code in each component.

DEMO CODE NOT MEANT TO BE MY ACTUAL CODE

// In Service Doc
crewCollectionRef: ....<Crew>;
crews: ...

constructor(private afs: AngularFirestore){
...
}
getCrews(){
  return this.crews;
}
// In Component
ngOnInit(){
  this.crews = this.service.getCrews();
}

I have been trying different ways to build a getCrews() method in a service file. But observable’s don’t seems to like being passed into some other component through a service. Is there any good blog post or even better perhaps a way to show how one might build services for Documents and Collections? I know this is something super easy but I am chasing my tail big time here.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

4reactions
callen5914commented, Oct 26, 2017

So I read this one. Thanks for taking the to share it. This is kind of confusing to me though. AngularFire is already mapping the data and ID to an Observable. I guess I was expecting this to be an Object that can be used.

routeCollection: AngularFirestoreCollection<Route>;
  routes: Observable<Route[]>;
  constructor(private afs: AngularFirestore) {  	
  	this.routeCollection = this.afs.collection<Route>('routes');
  	this.routes = this.routeCollection.snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as Route;
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    });
  }

  getRoutes(){
  	return this.routes;    
  }

and I get this error:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

when consuming like so:

routes = new Observable<Route[]>();

  constructor(private service: RouteService) {
  }

  ngOnInit() {
  	this.routes = this.service.getRoutes();
  }
1reaction
callen5914commented, Oct 27, 2017

@tja4472 I have two files here that I would like to share:

RouteInterface

export interface Route {
	id?: string,
	value?: string,
	display?: string
}

RouteService

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import { Subject, BehaviorSubject } from 'rxjs';
//import { Operator } from 'rxjs';
import 'rxjs/add/operator/map';

import { Route } from '../interfaces/route';

@Injectable()
export class RouteService {
 
  routeCollection: AngularFirestoreCollection<Route>;
  routes: Observable<Route[]>;
  constructor(private afs: AngularFirestore) {  	
  	this.routeCollection = this.afs.collection<Route>('routes');
  	this.routes = this.routeCollection.snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as Route;
        const id = a.payload.doc.id;
        return { id, ...data };
      });
    });
  }

  getRoutes(){
  	return this.routes;    
  }

}

I tried the extends method you demonstrated above and I get an error stating the id is not a property of Route

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularFire as a service (Not finding any documentation "On ...
I have been building an App using the latest releases and Angular and AngularFire. When using the documentation provided, I can do all...
Read more >
AngularFire firestore not getting fresh written document
I have a cloud functions that writes a user document, for example an access token Then after that function is called and awaited...
Read more >
Dependencies of Firebase Android SDKs on Google Play ...
These Firebase SDKs communicate with the Google Play services background service on the device to provide a secure, up-to-date, and lightweight API to...
Read more >
Google OAuth with @angular/fire - Fireship
Build a Firebase Google signin user authentication flow with @angular/fire that saves custom user data to the Firestore.
Read more >
File uploads come to AngularFire. Cloud Storage for Firebase ...
All without a line of server code. Get ready, uploading files from your Angular app just got a lot easier. AngularFire, meet Cloud...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found