Firestore 与 AngularFire 分页与 startAfter :Hn 不是函数或其返回值不可迭代

问题描述 投票:0回答:1

我正在使用以下版本开发 Angular 应用程序:

@angular/fire: 7.5.0
@angular/core: 15.2.0
firebase: 9.17.1
Node.js: v20.11.1

我正在使用 startAfter 和 lastVisible 实现 Firestore 查询的分页,预计它是一个 DocumentSnapshot。虽然这个逻辑在大多数情况下都有效, 我收到此错误:

TypeError: Hn is not a function or its return value is not iterable
    at index.esm2017.js:19940:33
    at Ul (index.esm2017.js:19951:5)
    at $l._apply (index.esm2017.js:19876:19)
    at Rl (index.esm2017.js:19608:30)
    at angular-fire.js:227:48
    at angular-fire.js:160:59
    at _ZoneDelegate.invoke (zone.js:372:26)
    at Zone.run (zone.js:134:43)
    at NgZone.runOutsideAngular (core.mjs:24159:28)
    at runOutsideAngular (angular-fire.js:160:35)

这是我用于分页的代码:

queryRef = 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)
);
angular google-cloud-firestore pagination angularfire2
1个回答
0
投票

这是我的代码;这可能有助于澄清问题

import { Firestore, collection, query, where, orderBy, startAfter, limit, getDocs } from '@angular/fire/firestore';

async function loadData(restaurantId: string, statusByPortalTab: string, requesterType: string, batch: string[], batchSize: number, lastVisible: any) {
    try {
        const baseQuery = query(
            collection(this.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")
        );

        const paginatedQuery = lastVisible
            ? query(baseQuery, startAfter(lastVisible), limit(batchSize))
            : query(baseQuery, limit(batchSize));

        const snapshot = await getDocs(paginatedQuery);

        if (!snapshot.empty) {
            const documents = snapshot.docs.map(doc => doc.data());
            lastVisible = snapshot.docs[snapshot.docs.length - 1];
            console.log('Fetched Documents:', documents);
            return { documents, lastVisible };
        } else {
            console.log('No more documents to load.');
            return { documents: [], lastVisible: null };
        }
    } catch (error) {
        console.error('Error fetching documents:', error);
        throw error;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.