0

I am trying to get the uid that Firebase generate for my new records/documents, the one shown here:

enter image description here

I'm trying to get a document, but since I don't have its id, I cannot reach it. This code won't return the records' uids, it only returns the records themselves:

return this.afDB.collection('games');

Also, I cannot use the id I generate to query it, and if i use a collection to be able to query it, it just won't let me update or delete the record.

So, this doesn't work:

this.afDB.doc('games/' + game.id).delete();

Is there a way to get that UID I am looking for?

Multitut
  • 2,089
  • 7
  • 39
  • 63

1 Answers1

0

I already solved it, this is my service code:

import { Injectable } from '@angular/core';
import {AngularFirestore, AngularFirestoreCollection} from 'angularfire2/firestore';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class GamesFirebaseService {
    private itemsCollection: AngularFirestoreCollection<any>;
    items: Observable<any[]>;
    countItems = 0;
    constructor(public afs: AngularFirestore) {
        this.itemsCollection = this.afs.collection<any>('games');
        this.items = this.itemsCollection.snapshotChanges()
            .map(actions => {
                this.countItems = actions.length;
                return actions.map(action => ({ $key: action.payload.doc.id, ...action.payload.doc.data() }));
            });
    }
    public store = (game) => {
        return this.itemsCollection.add(game);
    }
    public update(game) {
        return this.itemsCollection.doc(game.$key).update(game);
    }
}

Basically I had to map action.payload.doc.id to an attribute on every object, $key in this case. Then use it when I try to access that object.

Multitut
  • 2,089
  • 7
  • 39
  • 63