Ron Astle Lobo
Ron Astle Lobo

Reputation: 1314

How to order data based on a parameter in firestore?

I am facing an issue while Ordering data based on a parameter. Below is the code snippet.

event_date is a timestamp

 db.collection("abc")   
 .doc("Events")
  .collection("List")     
  .where("event_status", "==", "0")   
     .orderBy("event_date", "desc").get()      
     .then(function (querySnapshot) {-------------------});

The error message thrown is,

Uncaught Error: Objects are not valid as a React child (found: Sun Nov 25 2018 17:27:56 GMT+0530 (India Standard Time)). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Table.

Upvotes: 2

Views: 2414

Answers (1)

megha
megha

Reputation: 138

By default, a firebase query retrieves all documents in ascending order of document ID. You can specify the sort order for your data using orderBy().

In your case .orderBy("event_date", "desc") , if you set the field "event_date" using firebase serverTimestamp() for ex: .set({event_date = firebase.firestore.FieldValue.serverTimestamp() }) you would get the above error. Inorder to resolve this you may instead set it to new Date() for ex: .set({event_date = new Date() })

In Firebase, firebase.firestore.FieldValue.serverTimestamp() and new Date() both would result in a timestamp

Hope this helps.

Upvotes: 2

Related Questions