2017-10-18 63 views
0

我试图让火力地堡为我的新记录/文件的UID,这里显示的:AngularFire2 - 获取由Firebase生成的节点UID?

enter image description here

我试图得到一个文件,但因为我没有它身份证,我无法达到它。此代码将不会返回的记录的UID,它只返回记录本身:

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

而且,我不能用我生成查询它的ID,如果我用一个集合,以便能够进行查询,它只是不会让我更新或删除记录。

所以,这不起作用:

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

有没有一种方法来获取UID我在找什么?

回答

0

我已经解决了它,这是我的服务代码:

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

基本上我不得不action.payload.doc.id映射到在这种情况下每个对象的属性,$关键。然后当我尝试访问该对象时使用它。