I am trying to flip my query snapshot docs but nothing happens:
export async function getUserPosts(
userId,
getOldPosts = true,
startAfter,
limit = MAX_USER_POSTS_TO_RETRIEVE
) {
...
const query = userPostsRef
.orderBy("date", getOldPosts ? "desc" : "asc")
.startAfter(startAfter);
const querySnapshot = await query.limit(limit).get();
if(!getOldPosts) {
console.log("=== BEFORE ===");
querySnapshot.docs.forEach((prev) => {
console.log(prev.data().date.toDate());
});
// Flip the docs if we are fetching new posts
querySnapshot.docs.reverse(); <-----
console.log("=== AFTER FLIP ===");
querySnapshot.docs.forEach((prev) => {
console.log(prev.data().date.toDate());
});
}
...
}
When I just call my method like this:
getUserPosts("...", false, new Date()); // will execute an orderBy("date", "asc")
Console logs the following data:
=== BEFORE ===
2021-11-08T20:57:31.382Z // (oldest)
2021-11-08T20:57:32.816Z
2021-11-08T20:57:36.130Z // (most recent)
=== AFTER FLIP ===
2021-11-08T20:57:31.382Z // (oldest)
2021-11-08T20:57:32.816Z
2021-11-08T20:57:36.130Z // (most recent)
As you can see, docs are not flipped... I expect to get
=== AFTER FLIP ===
2021-11-08T20:57:36.130Z // (most recent)
2021-11-08T20:57:32.816Z
2021-11-08T20:57:31.382Z // (oldest)
as the result of the operation, and I don't understand why this is not working if the following code does:
const querySnapshot = {
docs: [
{
data: () => ({ timestamp: 1 })
},
{
data: () => ({ timestamp: 2 })
}
]
}
// Before flipping: [1, 2]
querySnapshot.docs.reverse();
// Expected: [2, 1]
querySnapshot.docs.forEach((doc) => {
console.log(doc.data().timestamp)
})