skip to Main Content
const querySnapshot = await getDocs(collection(db, "posts"));

Need to order querySnapshot by ‘eventdate’ and in ‘desc’ order.

const querySnapshot = await getDocs(collection(db, "posts", orderBy('eventDate', 'desc')))

Have attempted to implement this way but no success, the entries on firebase just stopped loading.

2

Answers


  1. Most likely you’re missing a composite index that is needed for the query (ascending and descending order are different indexes).

    To troubleshoot and fix this:

    1. Catch and log any errors that are thrown
    2. Check in the error message for a URL.
    3. Click that URL and it’ll take you to the Firestore console to create the index that is needed for the query.
    Login or Signup to reply.
  2. Let start data as current time and compare it with your time field as i did
    , then orderBy it in desc formate as you needed

    it is Firebase FireStore-Cloud Excercise which i was performed some days back
    but if you are using firebase RealtimeDataBase Then just syntax will change otherthan logic will same.

    let start = Date.now();
    
        try {
          await firestore()
            .collection(DB_Name.all_requests)
            .where('timeStamp', "<=", start)
            .limit(newcount)
            .orderBy('timeStamp', 'desc')
            .get()
            .then(async querySnap => {
              //You will data here
              })
             .catch(e=>console.log('error',e)
           
           }catch(err){
             console.log("error in fetching",err)
             }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search