So I've read numerous questions on SO about item conversions in Javascript/Angular but am not finding anything that addresses my issues. I'm pulling from Firestore as an object that can have copies. I then need to convert the object using a 1 to many conversion. The object coming from Firestore is a 'MultipleCard'. This 'MultipleCard' has a property called 'copies' which will be used to signify how many 'Cards' should get created.
postsCol: AngularFirestoreCollection<MultipleCard>;
posts: Observable<MultipleCard[]>;
cardsListObservable: Observable<Card[]>; //Not sure which to use
cardsList: Card[]; //Not sure which to use
constructor(private messageService: MessageService,
private db: AngularFirestore) {
this.messageService.add('Fetching cards');
this.postsCol = this.db.collection('cards');
this.posts = this.postsCol.valueChanges();
//?? Nothing I do here seems to work correctly. Most operations act on the array itself or do a 1 to 1 conversion
}
Component
<mat-list *ngFor="let card of cardsList | async"> //Or cardsListObservable
<mat-list-item>{{card.name}}</mat-list-item>
</mat-list>
How do I convert Observable into Observable or Card[]? For example I may have an array with the following 'MultipleCard's
[{ id: 1,
copies: 3},
{id: 2, copies:1}]
That should beconverted into an array of 4 'Card' Objects:
[{ id: 1, copyVersion:1},
{ id: 1, copyVersion:2}.
{ id: 1, copyVersion:3},
{ id: 2, copyVersion:1}]
I appreciate any insight!
Edit 1
Tried the following:
this.posts.subscribe((posts) => {
posts.forEach( post => {
console.log(post);
this.cardsList.push(post);
});
});
But get:
core.js:1350 ERROR TypeError: Cannot read property 'push' of undefined at eval (deck-list.component.ts:40)
Final Updated Code:
static fromMultiple(multipleCard: MultipleCard): Card[] {
const data: Card[] = [];
for (let i = 0; i < multipleCard.copies; i++) {
data.push(new Card(multipleCard));
}
return data;
}
this.postsCol = this.db.collection('cards');
this.posts = this.postsCol.valueChanges();
this.posts.subscribe((posts) => {
posts.forEach( post => {
const temp = Card.fromMultiple(post);
temp.forEach(tempCard => {
this.cardsList.push(tempCard);
});
});
});