【问题标题】:AngularFire2 - Get node-UID generated by Firebase?AngularFire2 - 获取 Firebase 生成的节点 UID?
【发布时间】:2017-10-18 00:54:41
【问题描述】:

我正在尝试获取 Firebase 为我的新记录/文档生成的 uid,此处显示的是:

我正在尝试获取一个文档,但由于我没有它的 ID,所以我无法访问它。此代码不会返回记录的 uid,它只返回记录本身:

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

另外,我不能使用我生成的 id 来查询它,如果我使用一个集合来查询它,它就不会让我更新或删除记录。

所以,这不起作用:

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

有没有办法获得我正在寻找的 UID?

【问题讨论】:

    标签: angular angularfire2 angularfire5


    【解决方案1】:

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

    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 映射到每个对象的属性,在本例中为 $key。然后在我尝试访问该对象时使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多