Below are some suggestions to troubleshoot and resolve the error:
1. Verify the Value of lastVisible
Ensure that the value you pass into the call to startAfter is a valid DocumentSnapshot. For example, if you get your snapshot from a query, it should come from something like:
const querySnapshot = await getDocs(queryRef);
const docs = querySnapshot.docs;
const lastVisible = docs[docs.length - 1];
Double-check that lastVisible isn’t null or undefined when you use it for the next query.
2. Confirm Matching OrderBy Fields
When you use a query cursor (startAfter, startAt, etc.), make sure that the document snapshot you pass was created from a query that ordered the same field(s). In your case you are ordering by "create_on" in descending order, so lastVisible must be from a snapshot that contains that field and is ordered the same way. If the query orders on multiple fields, you may need to pass an array of values instead.
3. Ensure Consistent Imports and SDK Versions
Since you are using the Firebase v9 modular SDK, double-check that you import the necessary methods and types from the correct packages. For example:
import { collection, query, where, orderBy, startAfter, limit, getDocs } from "firebase/firestore";
This should ensure that the functions and types (including DocumentSnapshot) work as expected.
4. Debug and Log the Snapshot
Sometimes logging the snapshot that you are using as a cursor can be very helpful. Add some logging to see what lastVisible contains before using it:
console.log("Last visible document:", lastVisible);
If the log output isn’t what you expect (for example, if it’s null or an empty object), then you may need to review how you obtain the snapshot.
Summary Code Example
Below is an example that demonstrates obtaining the last visible document and then using it to paginate the next query:
import {
collection,
query,
where,
orderBy,
startAfter,
limit,
getDocs,
QueryDocumentSnapshot
} from "firebase/firestore";
// First query to load data
const firstQuery = query(
collection(this.firestore.firestore, "Inventory_Transfers"),
where("is_deleted", "==", false),
where("restaurant_id", "==", restaurantId),
where("status_by_portal_tab", "==", statusByPortalTab),
where("requester_type", "==", requesterType),
where("branch_ids_requested_for", "array-contains-any", batch),
orderBy("create_on", "desc"),
limit(batchSize)
);
const querySnapshot = await getDocs(firstQuery);
const docs = querySnapshot.docs;
// Check that we have documents to paginate
if (!docs.length) {
console.warn("No documents found");
} else {
const lastVisible = docs[docs.length - 1] as QueryDocumentSnapshot;
// Next query for pagination
const paginatedQuery = query(
collection(this.firestore.firestore, "Inventory_Transfers"),
where("is_deleted", "==", false),
where("restaurant_id", "==", restaurantId),
where("status_by_portal_tab", "==", statusByPortalTab),
where("requester_type", "==", requesterType),
where("branch_ids_requested_for", "array-contains-any", batch),
orderBy("create_on", "desc"),
startAfter(lastVisible),
limit(batchSize)
);
// Execute the paginated query
const nextQuerySnapshot = await getDocs(paginatedQuery);
// Process the documents from nextQuerySnapshot...
}
Final Notes
The error message
TypeError: Hn is not a function or its return value is not iterable
typically indicates that an argument (like lastVisible) isn’t in the format Firestore expects. Verifying that lastVisible is a proper DocumentSnapshot (and not null or an unexpected type) and that your ordering matches the snapshot is key to resolving the issue.
If the issue persists after these checks, you might want to clear your local node modules or check for any mismatches in package versions related to Angular Fire and Firebase.