https://discord.gg/botpress logo
Pagination problem
# 🤝help
n
I've made a script so I can get some data from all conversations I have of my bot, but when searching using the nextToken method as a paginator, it's not working as expected and it brings different data each time I search for all those registries, leaving bellow the script using typescript and a screenshot that shows its difference of results.
Copy code
ts
import axios, { AxiosRequestConfig } from "axios"

const BOT_PRESS_URL     = ''
const API_TOKEN         = ''
const INTEGRATION_ID    = ''
const BOT_ID            = ''

const reqContinuously = async (config: AxiosRequestConfig<any>, field: string) => {
    const apiData: any[] = []
    try {
        const reqObj = axios.get(BOT_PRESS_URL + config.url, {
            headers: {
                "Authorization"     : `Bearer ${API_TOKEN}`,
                "x-integration-id"  : INTEGRATION_ID,
                "x-bot-id"          : BOT_ID,
            },
            params: {
                ...config.params
            },
        })
        const { data } = await reqObj
        apiData.push(...data[field])
        
        const { meta } = data
        if (meta.nextToken) {
            const nextSearch = await reqContinuously({
                ...config,
                url: config.url,
                params: {
                    ...config.params,
                    nextToken: meta.nextToken,
                }
            }, field)

            apiData.push(...nextSearch)
        }
    } catch (error) {
        console.error(error)
    }
    return apiData
}

const execute = async () => {
    const conversations = await reqContinuously({
        url: '/v1/chat/conversations',
    }, 'conversations',)
    console.log({ totalConversations: conversations.length })
}

execute()
https://cdn.discordapp.com/attachments/1256324645565628416/1256324645695787111/image.png?ex=66805b00&is=667f0980&hm=e2b88329399c9de2d3c569d330f85bc6973914224a62dea77424cd718aeb6b42&
@big-market-58184 , @bumpy-butcher-41910