I need to combine two collections (tokens and trends) based on their IDs, where each item in the result should include data from both collections. This means that the ID of an item in the trends collection matches the ID of the corresponding item in the tokens collection.
To achieve this merging of documents, I have devised the following function:
export interface TokenData {
created_utc: number;
image: string;
name: string;
}
export interface usersCount {
subscribers: number;
visitors: number;
timestamp: number;
}
export interface TrendData {
tokenInfo: TokenData;
users_count: usersCount[];
}
getTrends(): Observable<TrendData[]> {
const tokens = this.db.collection<TrendData>('tokens').snapshotChanges()
.pipe(map(docArray => {
return docArray.map(doc => {
const data: any = doc.payload.doc.data();
return {
id: doc.payload.doc.id,
...data
};
});
}));
const trends = this.db.collection<TrendData>('trends').snapshotChanges()
.pipe(map(docArray => {
return docArray.map(doc => {
const data: any = doc.payload.doc.data();
return {
id: doc.payload.doc.id,
...data
};
});
}));
const fj = forkJoin({
tokens: tokens,
trends: trends
});
return fj.pipe(map(items => {
return items.trends.map(trendItem => {
return {
tokenInfo: items.tokens.find(({id}) => id === trendItem.id),
users_count: trendItem
}
})
}))
}
My main goal is to merge data from two Firestore collections, but currently, the function is not returning anything. I am unsure if forkJoin is the correct operator for this task or if there might be some other issue causing the subscribed function to not fetch the data.