Rogin Neil
Rogin Neil

Reputation: 55

Google Datastore Pagination

I am trying to use the cursor to implement pagination but when I try to use the endCursor that is returned after my first query (queries 10 records), it gives me an error "invalid encoding". By the way I have a total of 16 records. I am expecting that on my next query, it will give me the last 6 records

Here's my code:

    router.get("/scan/history/query", async (req: Request, resp: Response) => {
        const userId = resp.locals.user && resp.locals.user.sub
        const pageCursor = req.query.cursor
        if (userId) {

            let mainQuery = dataStoreClient.createQuery(process.env.GOOGLE_DATASTORE_KIND_SCAN_RESULTS)
                .filter("userId", QUERY_FILTER_OPERATORS.EQUAL, userId)
                .filter("isDeletedDocument", QUERY_FILTER_OPERATORS.EQUAL, false)
                .select(["__key__", "scanDate", "scanKeyword", "scanFilter",
                    "hasRecord", "scanThreatStatus", "scanDuration",
                    "scanType", "scanStatus", "domainName"])
                .order("scanDate", { descending: true })
                .limit(10)
            if (pageCursor) {
                mainQuery = mainQuery.start(pageCursor)
            }
            const results = await mainQuery.run()
            const entities = results[0]
            const info = results[1]
            const hasNextPage = info.moreResults !== "NO_MORE_RESULTS"
            const pageResult = new PageResult(entities, info.endCursor, hasNextPage)
            return HttpResult.Ok(resp, pageResult)
        }
        return HttpResult.UriNotFound(resp)
    })

UPDATE: I tried this with thousands of records and my limit is still 10. It works perfectly for like 2 or 3 queries but when I tried to query for the fourth time, it throws me an error "invalid encoding"

Upvotes: 0

Views: 668

Answers (1)

Melody
Melody

Reputation: 41

I know this is old, but in case anyone else comes across this issue (as I just did), I was able to resolve it by encoding the cursor value using encodeURIComponent(). It looks like the cursor value occasionally contains a + character, which causes issues when not escaped in the URL

Upvotes: 1

Related Questions