Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to order by date? #283

Closed
justme1 opened this issue Jun 25, 2016 · 14 comments

Comments

@justme1
Copy link

@justme1 justme1 commented Jun 25, 2016

Hi,

This one must be really simple as everyone who uses Firebase must encounter this problem.

In my server I save items like this:
var db = firebase.database();
var ref = db.ref("users");
var userRef = ref.child("userA");
var newUserRef = userRef.push();
newUserRef.set({
Date : Date.now(),
Msg : 'test'
});

Now assuming I have 2 items in 'userA' I want to display the items in descending order so the last item that got 'pushed' will be the first to be displayed to the user.

I'm using:

    this.itemsFirebase = this.af.database.list('/users/userA');

And in the template:

     <li *ngFor="let itemFirebase of firebaseService.itemsFirebase | async">
                 <notification-item [item]="itemFirebase"></notification-item>
     </li>

How can I reverse the order so that the user will see the last item that was pushed as the first item in the ngFor?

I read about Querying List(https://github.com/angular/angularfire2/blob/master/docs/4-querying-lists.md) but I couldn't find what I was looking for.

Thanks!

@kazkis

This comment has been minimized.

Copy link

@kazkis kazkis commented Jun 25, 2016

Hi @justme1,
I don't know if it's the best way to do it, but currently I'm creating a new field like this

const date = Date.now();
...
date: date,
reverseDate: 0 - date,
...

and then ordering by "date" for ascending order, by "reverseDate" for descending order.

@justme1

This comment has been minimized.

Copy link
Author

@justme1 justme1 commented Jun 25, 2016

@kazkis, great thanks I've implemented this and it works.

Seems a bit hacky - if there is a better way to do it it will be cool - otherwise this issue can be closed.

Thanks!

@davideast

This comment has been minimized.

Copy link
Member

@davideast davideast commented Jun 27, 2016

@justme1 Make sure to use the firebase.database.ServerValue.TIMESTAMP instead of Date.now()

@davideast davideast closed this Jun 27, 2016
@justme1

This comment has been minimized.

Copy link
Author

@justme1 justme1 commented Jun 27, 2016

@davideast why use firebase.database.ServerValue.TIMESTAMP instead of Date().getTime() ?

@kazkis

This comment has been minimized.

Copy link

@kazkis kazkis commented Jun 27, 2016

@justme1 The Firebase TIMESTAMP is based on the server time while Date.getTime() on the client one. Using the server timestamp you can avoid lots of problems with user different locales or incorrectly set clocks.
For example if I use my client time to send you a message and my clock says it's 10:02 instead of 10:00, when you'll receive it the time shown would appear to be 2 minutes in the future for you.

@justme1

This comment has been minimized.

Copy link
Author

@justme1 justme1 commented Jun 27, 2016

@kazkis Agreed 100%.

Thanks.

@captaincole

This comment has been minimized.

Copy link

@captaincole captaincole commented Jul 9, 2016

Hey guys, I just implemented a solution also to this, using the mapping function.

this.itemsFirebase = this.af.database.list('/users/userA').map( (arr) => { return arr.reverse(); } );

this will return an Observable in reverse order.

@justme1

This comment has been minimized.

Copy link
Author

@justme1 justme1 commented Jul 9, 2016

@thielCole, very nice solution, makes sense.

Fortunately I l've stopped using angularfire2 due to issue

#295

@zompad

This comment has been minimized.

Copy link

@zompad zompad commented Jul 9, 2016

@kazkis, I got your negative solution working using Date.now() but when using firebase.database.ServerValue.TIMESTAMP (as suggested by davideast) I was not able to set this to a negative value as I presume this is because it is resolved server side.

Instead I used thielCole map reverse solution and it is now working.

Thanks everyone!

@ericel

This comment has been minimized.

Copy link

@ericel ericel commented Jan 20, 2017

In my case I had a field in the datebase called createdAt (createdAt: firebase.database.ServerValue.TIMESTAMP)
To retrieve I simply ask my list to query order by child

this.postList = this.af.database.list('post',  {
      query: {
        orderByChild: 'createdAt'
      }
    });

getPost() {
     return this.postList.map(posts => {
         return posts.reverse();
     });
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 22, 2017

order by timestamp not working ,
I have a field "lastUpdated" in "chatroom => $chatroomId => lastUpdated"

i am using "firebase.database.ServerValue.TIMESTAMP" in my lastUpdated , But when i use this query

this.af.database.list("/chatRooms", {
query:{
orderByChild : "lastUpdated"
}
}).subscribe((chatRooms) => {
console.log(chatRooms);
});

The result doesn't comes according to timestamp , neither asc nor desc.

@katowulf

This comment has been minimized.

Copy link
Collaborator

@katowulf katowulf commented Apr 24, 2017

Please submit your own issue and cite a link to this one if you think they are similar. We won't respond to issues tacked on to the end of closed discussions.

@profesori

This comment has been minimized.

Copy link

@profesori profesori commented Aug 27, 2017

Hi,
I want to order firebase data by date and i'm using firebase.database.ServerValue.TIMESTAMP when i store data to firebase :

const exp = new Experiment();
var dateTime = firebase.database.ServerValue.TIMESTAMP
exp.creation = dateTime;
exp.reverseCreation = 0-dateTime;
exp.name = name;

So i'm setting a negative exp.reverseCreation value of data to have a descend order.The problem is i can't store a negative firebase.database.ServerValue.TIMESTAMP cause incompatible types. Is there a solution for that ?

Thanks

@bresleveloper

This comment has been minimized.

Copy link

@bresleveloper bresleveloper commented Oct 10, 2017

@thielCole i was missing as FirebaseListObservable<any[]>
this.itemsFirebase = this.af.database.list('/users/userA').map( (arr) => { return arr.reverse(); } ) as FirebaseListObservable<any[]>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.