Google数据存储分页

时间:2018-12-11 12:14:57

标签: google-cloud-platform google-cloud-datastore

我正在尝试使用游标实现分页,但是当我尝试使用在我的第一个查询(查询10条记录)之后返回的endCursor时,它给我一个错误“无效编码”。顺便说一下,我总共有16条记录。我希望在我的下一个查询中,它将给我最后6条记录

这是我的代码:

    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)
    })

更新: 我用成千上万的记录进行了尝试,但限制仍然是10。它可以完美地用于2或3个查询,但是当我第四次尝试查询时,它会向我抛出“无效编码”错误

1 个答案:

答案 0 :(得分:0)

我知道这很旧,但是如果其他任何人遇到此问题(就像我刚才所做的那样),我都可以通过使用encodeURIComponent()对光标值进行编码来解决。游标值似乎偶尔包含一个+字符,如果未在URL中进行转义,则会引起问题