angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Can't get $key from firebase on angular 2

Open Englbach opened this issue 9 years ago • 2 comments

on service.ts `import { Injectable } from '@angular/core'; import { AngularFirestore,AngularFirestoreCollection } from "angularfire2/firestore"; import { Observable } from "rxjs/Observable"; import {Business} from "../Business"; import { Category } from "../Category";

@Injectable() export class FirebaseService{

//collection
private _businessCollection : AngularFirestoreCollection<Business>;
private _categoryCollection : AngularFirestoreCollection<Category>;

businesses: Observable<Business[]>;
categories: Observable<Category[]>;

constructor(private _af:AngularFirestore)
{
    this._businessCollection = _af.collection<Business>('businesses');
    this._categoryCollection = _af.collection<Category>('categories');
}

getBusinesses(){
   this.businesses = this._businessCollection.valueChanges();
   return this.businesses;
}

getCategories(){
    this.categories = this._categoryCollection.valueChanges();

    return this.categories;
}

} `

on ngOnInit ` businesses: Business[];

//Get data of business this._firebaseService.getBusinesses().subscribe(data=> { this.businesses = data; }); //Get data of category this._firebaseService.getCategories().subscribe(data=> { this.categories = data; }); }`

on html, I called it by *ngFor

{{business.$key}}

but it is null or underfine.

sketch

Englbach avatar Nov 28 '17 12:11 Englbach

You are using Firestore, no Realtime Database. There is no $key in Firestore.

You have to use snapshotChanges() in order to retrieve the id of the document: https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md#snapshotchanges

diegofin avatar Nov 28 '17 13:11 diegofin

https://stackoverflow.com/a/51459727/7204574

Jorciney avatar Jul 21 '18 20:07 Jorciney