i need to get data from Firebase in descending order .
When the component loads for the first time it happens fine.
fetchData(){
return this.af.list('/path/path/',{
query: {
orderByChild: 'createdAt'
}
}).map(arr => {return arr.reverse();});
}
The data from fetch ads is stored in a var and then displayed.
But now the problem is the var is modified at run time the user can update the var array , or delete or add to it .
And when ever any of these operations takes place the data is modified on the firebase db and that calls the fetch data again .
But then the reverse is again revesed rendering the whole thing use less.
By this i mean is after the first component load the var is 1,2,3,4. But then when we add to var instead of becoming 1,2,3,4,5 it becomes 5,4,3,2,1.
This is very confusing but i a tried lot but not able to fix this.
UPDATE
ngOnInit() {
this.display = this.route.snapshot.data['Auth'];
if(this.display){
this.showProgress = true;
this.user = JSON.parse(localStorage.getItem("user"));
this.service.fetchData().subscribe((data) => {
this.comments = data;
this.showProgress = false;
});
} else {
this.afAuth.authState.subscribe((data) => {
if (data) {
localStorage.setItem("user", JSON.stringify(data));
this.user = data;
this.service.fetchData().subscribe((res) => {
this.comments = res;
this.showProgress = false;
});
} else {
this.comments = null;
}
});
This is what happens on run time rest all are the crud functions of angular2firebase that happens when we add update or delete
I tried using pipes for the same but then it causes problem when getting the index so i dropped the plan as it was getting too complex
add(){
this.service.postComment(this.comment, this.user);
}
editComment(key:string,updatedComment:any){
const index = ((this.page-1)*5)+i;
const updated = this.comments[index];
updated.comment = comment;
this.service.editComment(updated.$key,updated);
}
deleteComment(key:String){
this.service.deleteComment(this.comments[index].$key);
}