What I'm using
- Angular
- Firestore
What I'm trying to achieve
Manipulate document data
Convert the returned date into something readable
What I have
I have a list of albums where i use snapshotChanges to return the mapped data
I have an album that's a AngularFirestorDocument
Questions
- How can i manipulate the returned date in my album component the same way I have in the albums list component?
Album List
So this component brings back a list of albums correctly, and I can manipulate the date before i return it so that the HTML displays the correct format.
export class AlbumsListCompoment implements OnInit {
private albumCollection: AngularFirestoreCollection<any>;
albumns: Observable<any[]>;
folderId: string;
constructor(
private readonly afs: AngularFirestore,
private activatedRoute: ActivatedRoute,
private router: Router
) {
// Look at the url for the Folder ID and set the local variable
this.activatedRoute.params.forEach((urlParameters) => {
this.folderId = urlParameters['folderId'];
});
// Album Reference "folders/folderid/albums"
this.albumCollection = afs.collection<any>(`folders/${this.folderId}/albums`);
// Get the data
this.albumns = this.albumCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
// Get the date from each album
var albumDateTimeStapm = data.album_date;
// Convert the unix value and format it based on the users locale setting
var albumDateISO = moment(albumDateTimeStapm).format("DD/MM/YYYY");
// Create a new 'name' to use in the HTML binding
data.formattedDate = albumDateISO;
const id = a.payload.doc.id;
return { id, ...data };
});
});
}
ngOnInit() {
}
}
Album
I'm not entirely show how to approach this component to perform the same manipulation. It isn't a list, it's a single document / album.
export class AlbumDetails implements OnInit {
folderId: string;
albumId: string;
private albumDoc: AngularFirestoreDocument<any>;
album: Observable<any>;
constructor(
private readonly afs: AngularFirestore,
private activatedRoute: ActivatedRoute,
private router: Router) {
// Look at the url for the Folder and Album ID and set the local variable
this.activatedRoute.params.forEach((urlParameters) => {
this.folderId = urlParameters['folderId'];
this.albumId = urlParameters['albumId'];
});
this.albumDoc = afs.doc<any>(`folders/${this.folderId}/albums/${this.albumId}`);
this.album = this.albumDoc.valueChanges();
}
ngOnInit() {}
}
Any help would be greatly appreciated :)