#smilelari-Invoices
1 messages · Page 1 of 1 (latest)
async function getLinesForInvoice(invoiceId: string, lastLineId: string | null): Promise<any> {
try {
const params = {}
const invoiceLinesCollection = []
if (lastLineId !== null) {
params["starting_after"] = lastLineId
}
console.log(invoiceId)
for await (const line of stripe.invoices.listLineItems(invoiceId, params)) {
lastLineId = line.id
// do something with line
console.log(line.id)
const invoiceLinesCollectionObject = {
invoice_id: invoiceId,
id: line.id,
amount: line.amount,
period_end: line.period?.end,
period_start: line.period?.start,
// more properties
}
invoiceLinesCollection.push(invoiceLinesCollectionObject)
}
return invoiceLinesCollection
}
catch(err) {
setTimeout(getLinesForInvoice, 1000, invoiceId, lastLineId)
}
}
So you have 13 line items in this invoice
I don't think the API will return you 1000+ items (Stripe doesn't even allow that much items), what make you think it returns 1000+ items?
That's the size of the collection when it's done. Must be something in my code. Thanks
I'm stepping through the for await and outputting line ids and I'm definitely seeing duplicates. Any idea why I might be multiples of the same ID?
in_1KAliUEwYEdSC6hP6rjyCsEP
ii_1KAltrEwYEdSC6hPWsMct611
ii_1K2g3UEwYEdSC6hPiErQ1nFf
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW
ii_1K2eQTEwYEdSC6hPfuoiwxvK
ii_1K2eQTEwYEdSC6hPHaZ7wxIE
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW
These IDs look unique to me.
ii_1KAltrEwYEdSC6hPWsMct611
ii_1K2g3UEwYEdSC6hPiErQ1nFf
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW
ii_1K2eQTEwYEdSC6hPfuoiwxvK
ii_1K2eQTEwYEdSC6hPHaZ7wxIE
ii_1K2g3UEwYEdSC6hPaHsK5Ozv
ii_1K2g3IEwYEdSC6hPhFYl2mBC
ii_1K2g3IEwYEdSC6hPRSY1h0PG
ii_1K2eQbEwYEdSC6hPqnGu4oGp
ii_1K2eQbEwYEdSC6hPgL1ptaSW
I highlighted 2 that are not
There are a couple other duplicates
I see, is it possible that getLinesForInvoice function was called more than once?
and is lastLineId != null?
lastLineId is null and it is only being called once
because I see you are setting the lastLine here lastLineId = line.id
It's null when it's called initially. It's only populated if it lands in the catch and retries
I don't see this with other invoices
I'll suggest to to simply remove the params here, I don't see a reason why you want to set starting_after in an auto-pagination.
ok
I was having periodic issues where an exception would be thrown while retrieving line items StripeConnectionError - and was trying to come up with a way to retry in that event
Or I guess continue where it left off
Still seeing the same behavior without the params
OK, can you send me the updated code?
async function getLinesForInvoice(): Promise<any> {
try {
const invoiceId = 'in_1KAliUEwYEdSC6hP6rjyCsEP'
const invoiceLinesCollection = []
console.log(invoiceId)
for await (const line of stripe.invoices.listLineItems(invoiceId)) {
// do something with line
console.log(line.id)
const invoiceLinesCollectionObject = {
invoice_id: invoiceId,
id: line.id,
amount: line.amount,
period_end: line.period?.end,
period_start: line.period?.start
}
invoiceLinesCollection.push(invoiceLinesCollectionObject)
}
return invoiceLinesCollection
}
catch(err) {
console.log(err)
}
}
https://dashboard.stripe.com/logs/req_a2W8NxDVeyHthk I can still see the starting_after in the request
I think the typescript wasn't transpiled. I'm still seeing duplicates. Looking at the logs it looks like multiple requests, which I assume is wrong
I also got a StripeConnectionError
type: 'StripeConnectionError',
raw: {
message: 'An error occurred with our connection to Stripe.',
detail: Error: write ECONNABORTED
Hmm, I still see the starting_after in a more recent request https://dashboard.stripe.com/logs/req_AhQS1va4j7mOxw
can you send me the full error details?
StripeConnectionError: An error occurred with our connection to Stripe.
at C:\Dev\saige-cli\node_modules\stripe\lib\StripeResource.js:519:15
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
type: 'StripeConnectionError',
raw: {
message: 'An error occurred with our connection to Stripe.',
detail: Error: write ECONNABORTED
at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:94:16)
at WriteWrap.callbackTrampoline (internal/async_hooks.js:131:17)
at writevGeneric (internal/stream_base_commons.js:134:26)
at TLSSocket.Socket._writeGeneric (net.js:785:11)
at TLSSocket.Socket._writev (net.js:794:8)
at doWrite (internal/streams/writable.js:375:12)
at clearBuffer (internal/streams/writable.js:521:5)
at TLSSocket.Writable.uncork (internal/streams/writable.js:317:7)
at ClientRequest.end (_http_outgoing.js:855:17)
at ClientRequest.<anonymous> (C:\Dev\saige-cli\node_modules\stripe\lib\net\NodeHttpClient.js:79:15) {
errno: -4079,
code: 'ECONNABORTED',
syscall: 'write'
}
},
I can't paste the whole thing, but it's attached
Seems like you are having a connection error.
https://status.stripe.com/ Stripe API is working fine at this moment
Check Stripe system status and see recent API status updates.
https://stripe.com/docs/ips this is the list of Stripe server IP addresses, maybe you want to check your network configuration and make sure they are not blocked.
So, I also see the starting_after in the request, however, I am not passing it anywhere. I notice that the id that is being passed is actually an ID for a subscription.
Hmm, maybe you want do some debugging of your code then.
This request has returned 250 records so far
Is there a better way to make this request? I need to retrieve all line items for an invoice
Of course, you can use the autopagingToArray https://github.com/stripe/stripe-node#autopagingtoarray
const allLines = await stripe.invoices.listLineItems(invoiceId).autoPagingToArray({limit: 10000})
Is this correct?
Yup it's correct
That call returned all 10000 requested lines. I have no idea why. But I will look at it more tomorrow