Home > Enterprise >  Ordering Docs Snapshot By Time Firebase V9
Ordering Docs Snapshot By Time Firebase V9

Time:12-16

I'm looking to order the docs that are received from Firestore from oldest to newest. I'm using JS getTime() to set a field within the docs I'm trying to order and im using this code:

onSnapshot(collection(db, 'guides'), orderBy('timeStamp'), (snapshot) => { console.log(snapshot.docs); });

I feel like im doing everything right and have spend ages trying to figure this out. What am i doing wrong?

CodePudding user response:

The following should do the trick:

const q = query(collection(db, 'guides'), orderBy('timeStamp'));
onSnapshot(q, (querySnapshot) => {
  querySnapshot.forEach((doc) => {
      console.log(JSON.stringify(doc.data())); 
  });
});

Note that we pass only two parameters to onSnapshot() (including the callback) while you pass three.

  • Related