I have a flatlist which displays all the posts of users that the current user is following. This is normal in instagram, twitter, all social networks etc.
I want to display them chronologically, but it is not working.
The code works like this:
- I query Firestore for the current user's posts
- I have a list of UID's of ever user the current user is following
- I query Firestore for the posts of whoever the current user is following
- This returns all the posts I want
- The posts are in blocks. Ex, current user's posts are added to the array. Then user1 current user is following's posts get added. Then user2's posts get added. Etc.
- I attempt to run a .sort function provided by Javascript to order the posts chronologically
Here is the code for it (removed the doc fields as they are not important, except date_created):
getCollection = async (querySnapshot) => {
const followingPosts = [];
await Firebase.firestore() <----------------------- Get current users posts
.collection('globalPosts')
.where("uid", "==", Firebase.auth().currentUser.uid)
.onSnapshot(function(query) {
query.forEach((doc) => {
const {
....other fields
date_created
....other fields
} = doc.data();
followingPosts.push({
....other fields
date_created
....other fields
});
})
});
querySnapshot.forEach(async (res) => {
await Firebase.firestore() <-------------- get following users posts, uid after uid
.collection('globalPosts')
.where("uid", "==", res.data().uid)
.onSnapshot(function(query) {
query.forEach((doc) => {
const {
....
date_created
....
} = doc.data();
followingPosts.push({
....other fields
date_created
....other fields
});
})
});
});
followingPosts.sort(function(a,b){ <-------- How I try to sort the posts by date created
return a.date_created.toDate() - b.date_created.toDate()
})
this.setState({
followingPosts,
isLoading: false,
});
}
Few notes:
The posts are fetching correctly (only the people that current user is following's posts show up)
The reason I am doing date_created.toDate() is because firestore timestamp objects are in nanoseconds and milliseconds. Whether I have date_created.toDate() or just date_created, it doesn't work.
I am aware that I can query firestore and order by date_created, descending in the query. But since the posts are being queried sequentially, this only orders the individual blocks of posts, not the entire array
I have tried putting the followerPosts.sort function INSIDE the query snapshot, after the for each. Not working either:
querySnapshot.forEach(async (res) => { await Firebase.firestore() .collection('globalPosts') .where("uid", "==", res.data().uid) .onSnapshot(function(query) { query.forEach((doc) => { const { ....other fields date_created ....other fields } = doc.data(); followingPosts.push({ ....other fields date_created ....other fields }); }) }); followingPosts.sort(function(a,b){ return a.date_created.toDate() - b.date_created.toDate() }) });
EDIT: more information on date_created:
Upon creation (Adding a new post to firestore), date_created is initialized like this:
date_created: new Date()Within firestore, the above method of initializing date created looks like this:
When I console log the date_created, I am returned a firestore timestamp object:
t { "nanoseconds": 14000000, "seconds": 1610413574, }
This is unusable for my purposes, so I convert this timestamp object using .toDate() when I pass the data to the flatlist:
<FeedCellClass ... other fields date_created={item.date_created.toDate()} />.toDate() converts it to this, which I can use for my purposes:
2021-01-12T01:06:14.014Z
Let me know how to solve this issue.

followingPostsjust before you run sort over it? I'm just checking to make sure there are in fact elements within it. I'm not sure what the error you're getting is, whether it's empty or you get data that isn't in order.