#cristobal_pagination
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
๐ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1351217310475620546
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
i may also add that something similar happens on the dashboard's feature list, the next page does not load.
EDIT: just checked this and seems to have been resolved.
nope, just dashboard
maybe it's a me issue, but that's the code I use to re-verify the situation
also, Hi!
just tested if you lower the limit, you get the same result.
I set a limit of 10, I got first 10 and then 9 and then it ended. with the same issues.
Hello, ๐
I'm stepping in as my colleague needs to go. Can you copy/paste the code you use to paginate the list of entitlement features here between three ` characters?
ok
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_KEY);
const limit = process.env.LIMIT ? parseInt(process.env.LIMIT) : 100;
async function main() {
const showEntitlement = (prefix, entitlement) => {
const entString = JSON.stringify(entitlement);
console.log(`${prefix} Entitlement:\n${entString}\n`);
}
let iteration = 0;
let hasMore = true;
let startingAfter = null;
const onlyActive = !process.env.ONLY_ACTIVE;
const templatePayload = { limit, archived: onlyActive };
while (hasMore) {
const payload = { ...templatePayload };
if (startingAfter) {
payload.starting_after = startingAfter;
}
const response = await stripe.entitlements.features.list(payload);
hasMore = response.has_more;
startingAfter = response.data.at(-1).id;
console.log(`Iteration ${iteration++}, hasMore: ${hasMore}, entitlements: ${response.data.length}`);
console.log('######')
showEntitlement('First', response.data[0]);
showEntitlement('Third to last', response.data.at(-3));
showEntitlement('Second to last', response.data.at(-2));
showEntitlement('Last', response.data.at(-1));
}
}
await main();
results are in reverse chronological order and you're doing starting after
so this is expected
Likely you want ending_before
But also it's best to use auto pagination if possible: https://docs.stripe.com/api/pagination/auto
I see thank you so much! It was my bad.
No worries
I wanted to add something, I went and tried what the docs say and yes, it works replacing starting_after with ending_before.
However I tried the auto pagination and I found the same problem, it assumes it's "paginating" chronollogically.
I plan to use the ending_before method because the code is already done in those parts in my project, but I don't know if I used the auto pagination feature correctly. And if I'm doing it right I think you might want to know about it.
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_KEY);
const limit = process.env.LIMIT ? parseInt(process.env.LIMIT) : 100;
async function main2() {
const showEntitlement = (prefix, entitlement) => {
const entString = JSON.stringify(entitlement);
console.log(`${prefix} Entitlement:\n${entString}\n`);
}
const onlyActive = !process.env.ONLY_ACTIVE;
const templatePayload = { limit, archived: onlyActive };
let i = 0;
let count = 0;
for await (const feature of stripe.entitlements.features.list(templatePayload)) {
const iteration = Math.floor(i / limit);
if (i % limit === 0) {
console.log(`Iteration ${iteration}, entitlements: ${count}`);
console.log('######')
showEntitlement('First', feature);
}
if (i % limit === limit - 3) {
showEntitlement('Third to last', feature);
}
if (i % limit === limit - 2) {
showEntitlement('Second to last', feature);
}
if (i % limit === limit - 1) {
showEntitlement('Last', feature);
}
i += 1;
count += 1;
}
}
await main2();
However I tried the auto pagination and I found the same problem, it assumes it's "paginating" chronollogically.
It does not in all my testing and usage of auto-pagination.
One way to quickly test this would be to use the exact code snippet we show here on the right side.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Ok, thank you again.