Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

firebase - AngularFire2 group data into array of objects Firestore collection

I have a collection called addons. The collection contains fields namely, name, desc, price and type.

I am straight away fetching the data with this.afs.collection<AddOns>('addons'); which returns AngularFirestoreCollection<AddOns>. I have below class file defined for data from Firestore Collection.

AddOns.ts

export class AddOns{
    name?: string;
    desc?: string;
    price?: number;
    type?: string;
}

export class AddOnId extends AddOns{
    aid?: string;
}

Since I need metadata too from that collection I have created AddOnId class which extends AddOns class. To get all the data I do it as below:

addOnsList: Observable<AddOnId[]>;

this.addOnsList = this.afs.collection<AddOns>('addons').snapshotChanges().map( x => {
  return x.map(data => {
    const addon = data.payload.doc.data() as AddOns;
    const aid = data.payload.doc.id;
    return {aid, ... addon}
  });
})

This post has mentioned the problem of unable to use groupby since data that will be returned is Observable of Array and also has an answer how to convert Observable of array to Observable of values using flatMap.

I then tried

const grouped = this.afs.collection<AddOns>('addons').snapshotChanges()
                    .flatMap( arr => Observable.from(arr))
                    .groupBy(d => d.payload.doc.data().type);

But even that does not return desired result. How else can I achieve this? How should I be grouping the data based on their type and turn it into an Observable of values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...