collection reference cast to angularfirestorecollection or observable

Viewed 1380

I have below code

service file datalink

private dbUser = '/users';
constructor(private firestore: AngularFirestore) {
this.userCollection = firestore.collection(this.dbUser);
}

In my component file

this.datalink.userCollection .ref.where("name","==","xxx").orderBy("updatedOn")

it returns CollectionReference,

How to Convert CollectionReference in AngularFirestoreCollection or Observable?

I got below link

Convert CollectionReference in AngularFirestoreCollection<T> or Observable?

But not able to convert my scenario, lack of knowledge being new in the era.

I know I can do this like below which returns per my requirement.

this.firestore.collection('collectionanme',ref)

But I want my above query to return AngularFirestoreCollection or Observable?

Please help

Thanks

2 Answers

If you want to return an AngularFirestoreCollection or an Observable, do this:

import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

export class MyClass {    
  dblist: AngularFirestoreCollection<any>;

  constructor(private afs: AngularFirestore) {
    this.dblist = this.afs.collection<any>('path/to/collection', ref => {
      return ref.where("name","==","DAL").orderBy("updatedOn");
    });
  }

  getCollection(): AngularFirestoreCollection<any> {
    return this.dblist;
  }

  getObservable(): Observable<any[]> {
    return this.dblist.valueChanges();
    // or return this.dblist.snapshotChanges();
  }
}

private dbUser = '/users'; user:any; Declare observable constructor(private firestore: AngularFirestore) { this.user(); } user(){ this.user = this.firestore.collection('Users',ref =>ref.where('name','==', 'xyz' )).valueChanges(); }

Related